Why GTM server container duplicates intercepted GA4 client events? - google-tag-manager

I have server-side tracking implemented using Google tag manager.
I noticed duplicate events coming in but not because my GTM web container sends duplicate events (verified in debugger).
The GA4 client in my GTM server container receives two identical event calls, for no obvious reason. The only difference is the param _s:
https://www.google-analytics.com/g/collect?[...]&_s=1
https://www.google-analytics.com/g/collect?[...]&_s=2
I've dealt with this by creating a query parameter variable:
And then excluding all calls where the _s param equals 2 from my GA4 event trigger:
This solves the issue but seems like a hacky solution.
I wonder why two identical server events get triggered in my GTM server container, for a single GTM web event, in the first place?

I had a similar problem, and I couldn't solve it by filtering the parameter because it happened later (I think the params of the two events were _s=6 and _s=7).
I started looking for blog posts until I've found this one: https://www.thyngster.com/google-analytics-4-events-demystified-ga4. The suggestion that helped me was this one:
You may see events being fired on the browser that you didn't define on Google Tag Manager or GTAG. This is normal, don't go crazy with it. If you see a duplicate event or a new event that you don't know where it's coming from take a look at the Data Stream Settings
And, in fact, inside Data Streams was the solution to my problem: under Events settings > Create Events, someone had created a custom event based on the same name as the one I was firing through code, in my case it was add_to_cart.
Disabling the custom event solved the issue.
EDIT
I forgot to mention that the first way of debugging this issue was to check the value of the dataLayer variable in the browser console. This variable keeps track of all the events, together with their send_to attribute. Before looking into the analytics dashboard you should check whether you are sending duplicate events from the code and whether you are firing other events with the same name but without the correct send_to attribute, which will result in two events with the same name firing to the stream you are tracking.

Related

Blocking GTM after it is executed without refreshing the page

We have an app for cookie management and we can't seem to find a way to block/disable the GTM and its tags after it was initially executed/enabled without refreshing the page.
Basically, what we require is, if all of the cookie categories are initially enabled and therefore GTM is running initially, and after that, the visitor choose to change his cookie preferences and block all of the cookie categories, there is no way to block/disable the GTM(and his tags) without having to reset the page. The only workaround for that is to block the GTM script before it is loaded, but this requires a page reset.
We are looking forward to your reply.
There is a way to disable GTM, or to be more precise, to break the GTM instance running on the page.
GTM requires a "dataLayer" variable to work, that is an array of objects that communicates values from the frontend to the javascript that is the actual "engine" of client-side GTM. The datalayer is an array of objects.
GTM overloads the "push" method of the datalayer array with a custom function that, amongst other things, scans for the "event" key in datalayer objects. When it finds the event key, it updates the internal state of the variables an executes all triggers that are connected to that specific event.
So if you break that function, by re-initializing the datalayer variable or by overloading the push method again e.g. with a function that returns a null value, you effectively cripple GTM to a point where it can no longer execute tags.
datalayer.push = function() { return null; }
This will not unload the file (unloading JS is not possible without a reload), it will not purge GTM from memory and will not remove anything that GTM has injected into the page up to this point. This is a bit of a hack, and the datalayer variable name is configurable, so you need to check first how the datalayer is actually called. But breaking the datalayer variable (actual implementation might need a bit more work, but that is the principle) will stop GTM effectively from interacting with the page in any meaningful manner.

Can We Use "Message" as an (custom) Event to Fire a Tag in Google Tag Manager?

I have an id being pushed to my data layer via dataLayer.push() and there is no event key at the moment.
I know the best practice is to use an event key like 'event': 'idPush' and then in Google Tag Manager UI, have a trigger that activates when the custom event idPush occurs.
Is there is a way that I can still get the trigger to activate upon seeing a generic 'Message'?
I can't be 100% sure because I haven't read the GTM source code and I couldn't find any articles that talk about this, but I'm reasonably certain that this can't be done.
I tried:
creating a Custom Event trigger with a regex match of .* which would match anything, including nothing.
matching undefined, because according to the preview pane, the _event variable is set to undefined for Message events.
Unfortunately neither of these worked, and preview mode just says No tags were evaluated for the Message. This leads me to think that GTM only checks to see if any triggers should fire when an event is pushed into the dataLayer.
No. Until there was an event, GTM does not know about the content of the message - the message is just the debugger telling you that something has been added to the global dataLayer variable (which exists in the global namespace of the browser, not the confined namespace of GTM). The even is what updates GTM's internal state to make it aware of changes and additions to the dataLayer.
Depending on your use case you might be able to use a trigger type that creates it's own event - e.g. setting a visibility trigger to an element that you know will be at the viewport after your message, which will then take the new values of the dataLayer into account. Or create a custom HTML tag with a setInterval functions that periodically pushes an event to the dataLayer.
While these solutions may work, I do not think they are actually good. Finding a solution to change your page code will almost certainly be less headache in the long run than using a workaround.

Push to Enhanced Ecommerce after dataLayer has already been set and GTM.js script has run

I'm using Google Tag Manager and implementing Enhanced Ecommerce tracking via dataLayer. Everything is working fine. However, I'm now adding a CTA (call to action) that I want to track impressions for (there's multiple versions). This CTA is rendered as a partial, so at the time dataLayer is being constructed, it doesn't exist yet and I have no idea what will end up being there.
With straight Google Analytics, it looks like you can manually track an impression via:
ga('ec:addImpression', {
// impression data
});
But, this doesn't work with GTM, as ga is not defined in that scenario. According to the GTM Enhanced Ecommerce documentation, the only other "option", is to manually track the impression through the GTM control panel based on page view. Again, that's not feasible as the impression data is not always the same.
After a little research, I found a third "option" in delaying pushing dataLayer. For example, instead of letting if fire on GTM load, you can tie it to a particular event and then send that event at a later point. I suppose that would let me, then, alter the dataLayer in this partial, as long as I made sure that the event was not sent until well after it's been rendered. However, that not only seems clunky and prone to error, but it would also require me to substantially alter the rest of my Enhanced Ecommerce tracking code.
Is there no way to just send the impression as you can with straight GA, with GTM?
I am not quite sure that I understand the problem (but I have a go anyway). The way to do this is to push new data to the datalayer and have a custom event, i.e. the keyword "event" within the datalayer with a custom value; you then can use a "custom event" type trigger with the value you have pushed:
dataLayer.push({
"event":"mycustomevent",
"impressionData": myData
})
Specifications for EEC dataLayer are here. You then send a GA event that has EEC enabled and reads EEC data from the impressionData variable in the example.
You seem to have figured that out in principle, but seem to think this is somehow bad. No, it isn't, it is the recommended way by Google.
The dataLayer is an array of objects, and you can add new data to it by using the push method. This is not the native push method, but a custom implementation by Google. It scans the added data on every push, and if it finds the "event" key in the added object it adds the data from the on-page dataLayer variable to GTMs internal data model, where you then can use it in your tags.
Your problem might be, if I understand correctly, that you seem to think you have to construct all of the datalayer in a single go. But it is perfectly fine to split it up into multiple pieces and add new data via dataLayer.push as you need it.

Can I use same GTM tag to fire for multiple GA accounts?

I am working on one project which needs to fire same GTM tag to for two different GA account is this possible? or should I have duplicate all the same tags for two GA accounts?
There is no standard way to fire multiple properties in one tag. However I can think of two possible workarounds.
(Updated: The same questions came up on the GTM forum and naturally Simo Ahava had a solution).
One would be to create the tracker object manually in a custom html tag (the actual pageviews happen still via Ga tag templates). This would allow you to use Google Analytics plugins and there are plugins (e.g. here or here) that send hits to multiple properties.
The other workaround is to use a feature called tag sequencing. This has the advantage that you do it with "pure" GTM instead of GA plugins. I will first show the steps and explain what happens later.
The Setup
First you need to set up your tracking id as a variable of the datalayer type. As default value you set your first tracking id:
Next you create a custom html tag that will later set the second tracker id. This also sets a custom event necessary to fire the second tracker. Note that there are no triggers attached to this tag, and that it is set to "fire only once per page" (the last thing is very important! You can set this in Advanced Settings -> Tag firing options).
Then you create a trigger that uses the custom event from the previous step:
Now comes the magic with the GA tag. You use the variable for the tracking id that you have created earlier. Then you go to "Advanced Settings", expand the "Tag Sequencing" options and check the mark before "fire a tag after fires". You select the custom html tag from above (named set2ndId in my example). Then you attach a pageview trigger and the custom event trigger (called 2ndTracker in my example).
The sequence
GTM is loaded and evaluates the tracker id from the default value of the datalayer variable
The pageview trigger fires the GA tag
The tag sequencing setting fires the custom html tag that sets the datalayer variables for event and tracking ind
Since we have an event the datalayer is re-evaluated, and the new value with the second tracker id from the custom html tag is set
The custom event trigger fires the GA tag with the second tracker id
Since the custom html tag was set to "fire once per page" it is not fired again (else you would end up in a loop)
A word of caution: This is somewhat clever, even if I say so myself, but at the moment it is a bit "proof of concept". I use it without problems for pageview tracking, but I'm still working the kinks out for event tracking etc, so you have to decide for yourself if this is useful for you.
But at least it answers your question: yes, it is possible to send to multiple properties without duplicating the GA tags, but it takes some additional setup so you have to decide if this actually saves you work.
yes it is fine to fire multiple GA properties in single GTM.
Maybe this tutorial will help you:
http://www.kristaseiden.com/step-by-step-adding-a-second-ga-property-via-google-tag-manager/
I hope this helps.

Analytics User-ID view not tracking events

I've created a new view in Google Analytics with everything set up the same as a currently functioning one, only with the addition of User ID tracking turned ON. It's using the same property so the UA code has not changed.
I've copied over everything that I can think of from the current view to the new view. At first glance everything was working fine, page views were coming through via Real-Time correctly and matched up with the values seen in the current view.
For some reason though, the new view is not listing ANY actual events in Real-Time, however the events graph is being populated.
The current view IS tracking all the events correctly.
We use Tag Manager to handle all our events among other things, and using the debug mode, all events were also being triggered correctly. Finally, I've also used the GA Debugger chrome extension, which again is not showing any issues. The new User ID property is being passed through to analytics as I'd expect.
I've tried searching for any issues related to User ID views and tracking events, but came up blank, presumably because there is no inherent issue with this set up.
Any suggestions?
I found out that the reason that the events were not tracking is because the user id was not being sent along with every event.
I believed that once it was set on page load every analytics interaction after that would use that user id. This was not the case. In Tag manager I added a 'Field to set' property for all relevant tags like so;

Resources