Fill ExperimentID and ExperimentVariant without Google Optimize - google-analytics

We have a server side experiment running through our own implementation.
We started with Google Optimize to keep track of the experiment through their server side experiments-solution but some tracking seems to be off for us (not so standard setup). Now we want to test whether the problem is with our Optimize implementation.
What we would like to do is fill the experimentId and experimentVariant in the fieldsObject without using the implementation with Google Optimize.
We're using Tag manager for our GA implementation and while debugging we do see that the experimentId ("expId") and experimentVariant ("expVar") are being set but for some reason it's not being picked up by GA in our reports.
This is what I see in the GA Debugger for the other variant, this seems fine:
Is it possible to set expId and expVar in GA fieldsObject without using Google Optimize? If so, how and how do I see them in my reports?

If you are using GTM to also deploy the Universal Analytics tag - your tracker would not have the default tracker name.
Thus, the code ga('set', 'exp', '{expId}.{expVar}') (which implicitly assumed a default named tracker) would not set the fields on the tracker it should.
Instead, use the Optimize GTM tag, and fire it right before you fire the UA "Page View" tag - and set the ExpVar and ExpID explicitly as fields on the UA tag.
(get those values from JS vars, DL or cookies for example, after the server decided which variant and experiment the user should be bucketed to).
See these questions:
Google Tag Manager & Optimize Server-Side experiment sending variation
How can I set Analytics variables with Google Tag Manager from the server?

Related

Prevent Google Tag Manager from firing tag on localhost

I've successfully installed Google Tag Manager on my website. I've started firing some events from Google Tag Manager and can see them come up when using the tag assistant debugger. I've set up tags which send events to Google Analytics and can see that data pipeline working well.
What I'd like to do is have an easy way to configure all my tags of type GA4 Event to not fire when the trigger happens in a non-production environment. I'd like my triggers to work in non-production environments (so that I can debug my triggers), I just don't want the events to be piped to Google Analytics in non-production environments. Is there an easy way to set that up in Google Tag Manager?
I've noticed that when running the tag assistant debugger there's a field called debug_mode which is set to true. I tried setting that in my local environment like so:
export function maybeSetDebugMode() {
if (typeof window !== 'undefined' && process.env.NODE_ENV !== 'production') {
window?.dataLayer?.push({"debug_mode": "true"})
}
}
but it seems that even with this code, my custom events continue to get piped to GA:
Broadly speaking, is there a way to configure Tag Manager to not pipe events to GA when a certain condition is met (like a URL match or some other event field having a specific value)? Alternatively, can Google Analytics be configured to ignore events during ingestion which meet some criteria?
Yes, there are pretty elegant solutions for not sending events to your prod GA from lowers or from previewing/debugging. And yes, there is a way to prevent GA from accepting those events.
I'll start from the former since it's much more proper.
Typically, to start sending events to GA4 from GTM, you have to have at least one GA4 configuration tag that asks you for the measurement id. Well, no one forces you to just give it the measurement id. Give it a CJS instead:
And then just reference it in your config tag like so:
Generally, this should do what you want. Whenever you're in your lower env, or when hostname is localhost, well or 127.0.0.1, or when you're previewing the container on prod, or whatever else you feel proper there, you will see your tags and triggers firing, but your associated events will be flowing into a different GA4 property.
About that different GA4 property. It is a good practice to still collect your lower env events rather than sending them to a non-existent property. There will be time when you want to see some testing data. Just don't keep it in one property with your prod data, but you know that.
Ok, now let's move to the second option. GA4, of course, has data filters. The filters are premature comparing to the old filtering system in GA UA like most of the GA4 features, however, they somewhat fit your needs. In particular, the developer traffic filter. You'd have to add a custom parameter to every developer event that you would like to be visible in debug view, but not present in the reports.
Given how raw and buggy GA4 is and how the number of filters in there is now severely limited, I would advise to solve it in GTM and keep the filters for things that couldn't be solved through GTM. Things like analytics spam, if you ever get it.
GTM has built-in vars that you can reference. But most of them are disabled by default. Here:
and then when you type {{ and start typing the var name in your CJS, you will see this:
just click the option that you need and it'll autofill for you.
and you're supposed to treat it as whatever that referenced variable returns. It's mostly strings. but you can definitely return a funciton and run it like {{cjs that returns a function}}(param1, param2). Anyhow, we're doing strings, so, here:
should be pretty comfy to use. The only awkward thing with referencing the built-in vars is that it's hard to debug them in the console, so I often just take my hostname from globals, like window.location.hostname.

Google Tag Manager - (enhanced) e-commerce - how to load Javascript after <body> tag

I'm still learning Google Tag Manager and Enhanced E-commerce tracking, but I have a few questions, some of which may be pretty basic:
We're using a site on a third party system - I already have Google Tag Manager in my template within the HEAD tag, working with Universal (Google) Analytics. However, we want to just get the e-commerce transaction (purchase) data into Analytics - so basic e-commerce tracking would be fine, but I'd rather use Enhanced E-commerce tracking since it is newer - even though my immediate requirement is just to track purchases.
So, I need to send push transaction data into the dataLayer, but I can't do this due to restrictions on the system. I can only add special Javascript code to the end of the BODY tag for my "checkout complete" page.
Reading this online, the dataLayer must be defined in the HEAD before the GTM code is in place. As I cannot do that, I need to load the data using Javascript at the end of BODY.
Is this possible, if so how? I think I can do this using a Custom Javascript Variable as mentioned here: https://developers.google.com/tag-manager/enhanced-ecommerce#macro
Is a "macro" the same as a "variable" as explained in the above link? It says "macro", but Google Tag Manager allows me to define a custom "variable" which is "Custom Javascript". Just checking that these are one in the same.
Where would this code actually "run", if I put it in Google Tag Manager? The third party system defines some variables for me during script execution and I don't know where/when (during page load of the checkout thank-you page) it does it, so to be safe, I'd rather have this script run at the end of the page before the BODY tag closes - if I were doing this manually using plain Javascript within the HTML. In Tag Manager, how would I tell the Javascript to run "at the end of the page"?
Thanks in advance!
1 - Use dataLayer.push() in your Custom Javascript at the end of the BODY. Include an event in this with the transaction data.
For Example:
dataLayer.push( 'event' : 'custom.transaction', 'ecommerce': {} );
You can see the ecommerce object structure here:
https://developers.google.com/tag-manager/enhanced-ecommerce#purchases
dataLayer.push() is used after Google Tag Manager script has been included on the page, so you won't need to define it in the head of the page. Just translate your available ecommerce data into the structure required.
You then need to set up a Custom Event trigger in Google Tag Manager that uses/matches your event value/name.
Then create a Universal Analytics Event Tag that uses this Trigger. Set what ever you would like for the Category, Action and Label. Under More Settings > E-commerce, check Enable Enhanced Ecommerce and then check Use dataLayer.
This event is simply used to pass the Ecomemrce Data into Google Analytics.
2 - Macro is the same as Variable. You are correct Custom JavaScript Variable is what you are after.
3 - The code lives in Google Tag Manager Variable scope and is executed when the variable is used and/or with Google Tag Manager state changes. You can't make this code run at the end of the page. Also this code would need to access/scrape values from the page. To ensure the data is available you would only use this Variable on a Tag that is set to trigger on Window Loaded / DOM Ready, depending on when the on page data is ready.
I would suggest using dataLayer.push() and only use the Custom Javascript Variable approach if dataLayer.push() is not possible.

Switch from Google Analytics to Tag Manager

I have site which run on Google Analytics for some time( more than 1 year), now I want to use Google Tag Manager. If i remove Google Analytics from site, and start tracking with tag manager, there is a risk of losing data?
It's really hard to give a helpful answer to that - if you manage a seamless switch between GTM and inline GA code, and have set up everything correctly then you will not lose data. Is there a risk you make mistakes ? Probably.
I recommend to integrate GTM alongside GA and set up your GA tags so that they track to a test property. Then you can compare both properties to make sure that you track actually the same things. And that point you can throw out the inline GA code and switch to GTM (if you implement the tracking id as a variable in GTM it will be as simple as changing a single variable).
But remember to actually remove the inline code, else you will end up with double pageviews.

Switch to Tag Manager, will Enhanced Ecommerce still work?

I've successfully implemented Google Enhanced Ecommerce on one of my websites, where I also have a Google Universal Analytics tracking code. Now we want to change to Google Tag Manager, thus replacing the UA tracking code with the Tag Manager Code (correct?)
Will this affect the Enhanced Ecommerce tracking in any way? Or can I just replace the tracking snippet and the ga('create')-parts with the Tag Manager-snippet?
You need a correctly set up dataLayer (which rather more complicated since you need info on product impressions etc). Documentation is here:
https://developers.google.com/tag-manager/enhanced-ecommerce#enable
Then you create a news Analytics Tag (just a pageview or event, there is no need for a separate transaction tag) and select "Enhanced Ecommerce" with the "Use datalayer" option in the advanced settings.
GTM will pick up the values from there datalayer and do the rest for you.
So yes, you can get this to work, but it requires a lot of preparation for the correct datalayer implementation. Simply replacing the GA code with the GTM code will do very little except breaking your tracking.

Google Tag Manager + Google Analytics v just using GA: any reason to use just GA?

I am using Google Tag Manager on all my sites now to implement Google Analytics and future proof them for any other scripts.
I am putting GTM in my boilerplate.
Is there any reason this might not be good practice?
Any reason why a website (that needs GA) should avoid Google Tag Manager?
Most websites will require some sort of Javascript code added in the future for affiliate tracking, various analytics and having GTM installed will allow for easy installation of any such JS code easily.
Or, as Google puts it: "Why wait months for site code updates? Google Tag Manager lets you launch new tags any time with a few clicks, so you never miss a measurement or marketing opportunity."
Since GTM does not come with a service level agreement you could (very very tenuously) argue that GTM adds an additional point of failure. And if one wanted to be pedantic one could point out that not all ways of analytics tracking work with GTM (if you track serverside via the measurement protocol).
But real life argument, there is none (IMO).
There might be pages that do not greatly profit from GTM (or any other Tag Management) if all you do is to deploy a single analytics tag to track pageviews. But the second you need to track an event or pass data GTM is already worth it.
This is not meant to be merely opinion based, it's more that in 2,5 years of using GTM on large sites I have been unable to find any scenario where the tag management code has caused any technical problem or interfered with existing code. On the other hand I do not write click handlers or submit handlers anymore, I have a boilerplate template for a container tag in which I just have to replace values for a few macros before I import it to GTM and have tracking up and running, I can set data fields with much less trouble than via the code... so I think there is a real technical argument to make in favour of GTM, and none against it.

Resources