I am developing a widget that can be added to a host website via snippet of js code that once rendered will add an iframe to the host website.
Now my customers want to give me their GA tracking id so that I can send events from my widget to their GA4 account.
How should I handle this ?
- Add GA4 tag to the widget code and send the events from within the iframe, regardless of any GA4 tag that the host / parent page has
- Post messages from within the iframe page to the parent window and then intercept them on parent window and send them to GA4 from there. I will have to check if the parent window already has GA4 tag and if so, use it. If not, I will have to add the tag to the parent window.
Couple of things to note:
- My customer has added my widget code into their website so I can do dom manipulation on their site.
- My widget iframe is running on my domain which is different from my customer’s parent window domain
- My customer might or might not have GA4 tag in their parent window.
4
Ok, how you should handle it:
-
You don’t add the GA to the widget code. Your widget code deploys the iframe.
-
No, you don’t send the post messages unless your stakeholders explicitly asked you to do so. Well, you may. Some iframe widgets would be nice enough to provide JS events to the parent. That’s handy.
What usually happens in these cases is the customer would give you their GTM snippet and you would deploy it in the iframe. Not in your widget.
Then they will be able to track whatever they want through that GTM snippet. That’s very simple on your part.
Another thing they could ask for is to give them dataLayer events, but in this case you don’t need their GA id either. You just do the post messages exactly as you described from the iframe to the parent, where your widget would deploy listeners and translate those messages into the datalayer events. If you do this, you’re a very nice and considerate widget. I wish all widgets were this good.
Finally, your client may not have the capability to do any kind of tracking on their side. In other words, they saved money on technical analyst expertise. In that case, they would ask you to send actual events into their analytics, at which point you become that technical analyst. And that’s… I wouldn’t expect any widget vendor to do this. That’s a bit too much to ask for many reasons. One of which, the vendor doesn’t and shouldn’t know about every client’s data structure and logic. Doing separate tracking implementation for every client is nonsense. Hence the dataLayer is the middleground where they can get the events and remap them into their analytics as they see fit.
2