Is there a way to read the value of a Tag Manager variable in the client? - google-analytics

We are setting up some server-side Google Analytics events and we recently transitioned our GA implementation to use Google Tag Manager. I would like to maintain some of the constants we use in GTM so that they can be controlled from there. A good example is which GA Tracking ID to use. We currently store this in a lookup field called "GA - Tracking ID Lookup".
We would like to find a way to retrieve the value of this GTM variable in javascript code on our site. Is this possible?

I think the best way for you to manage this is to use the Data Layer. Your JS will be able to read the values in the Data Layer and GTM will also be able to.
If you were to go this route, you'd change your lookup variable to a datalayer variable for GTM.
Examples:
Have your server write out a DOM element to contain the analytics tracking ID something like
<meta name="ga-tracking-id" content="UA-1234567890-1" />
You can also store the same information into the dataLayer:
<script>
dataLayer = [{
'ga-tracking-id': 'UA-1234567890-1',
}];
</script>
This data can then be retrieved using the DataLayer variable type in GTM.
Another way you can manage this through one GTM container is to create a lookup table in GTM, then depending on which hostname, use a different tracking ID.
Though I'm not sure how you're managing this, but it also could be easier to just use different containers all together instead of varying tracking ID in one container.

Related

Can i configure two different GA4 Tags config for one GTM?

Can I track same event tags from GTM for different GA4 Tags?
I would like to have another GA4 for the same application but different environment.
If I configure new GA4 config tag, GTM should send data properly for both GA4's?
Currently not. You need another ga4 event tag to fulfill this requirement.
But I think the easiest way is :
Create another GA4 configuration tag.
Export the current GTM container.
Copy all the tag in the JSON file and replace the measurement id to the new one.
Now you have two set of the GA4 events.
Downside for this is you need to maintain 2 set of GA4 tags in the future.
Try using a lookup table variable for your measurement ID for your configuration tag. For the lookup table variable, put your input as the page hostname for each environment and then output to each data stream measurement ID from GA4. That way you can just use one config for the different environments and then have one event tag that maps to the config tag.

Why GTM restirct tag deployment doesnt work

I'm currently working on blocking the scripts injected by gtm and I noticed there's a way to add allowlist or blocklist inside datalayer to reach that: https://developers.google.com/tag-manager/web/restrict
But after trying to add the gtm.allowlist/blocklist, it still injects the 3rd party scripts
Anyone who had any experience on that? Many thx
The best way to allow the delivery of a tag is to associate the correct trigger based on the classification of the cookies you assign.
With OneTrust (you are using) the cookie (or the OnetrustActiveGroups variable) that contains the information of the accepted cookie classes will contextually contain the values ​​C0001, C0002, c0003, C0004, ... If for example the category C0004 is for cookies may be set through our site by our
advertising partners, to GTM Facebook tags, Ads tags, etc ... you will assign as a trigger the check that in that variable there is that value. If it is present the tag is fired otherwise not.
In this way everything is much more controlled and you leave nothing to chance or to the interpretation of the tool.

Access GTM google tag manager variables from outside of GTM?

I was reading about GDPR and GTM here https://www.analyticsmania.com/post/gdpr-cookie-consent-notification-with-google-tag-manager/
basically how to make GTM gdpr valid.
the issue I saw, was that I don't know how to read any GTM variables from outside of GTM. E.g. in that guide, they store a variable or function that will figure if consent was given or not. How can I read, utilize functions and react to events from outside of GTM?
say I run my little javascript file myapp.js on the page. In there I want to for example not add youtube video embeds in case of they have not given consent. Or add videos and share links if they gave consent. This is not something I can do via GTM obviously.
Well the dataLayer variable is public so you can read it at any time it just might be a little hard to find an specific value since you need to move though an array.
If you want to use a javascript variable on your site as a GTM variable then the variable needs to have global and not local scope (the dataLayer is global but you need to perform a dataLayer.push() to add items to the array).
If the javascript variable on the site is responsible for other site functions and has global scope, then either reference it using a GTM javascript variable or custom javascript variable and use it to act as the trigger exception.
The alternative is to have the site logic prevent the GTM snippet from loading. In the GDPR perfect world, you would have granular opt-out depending on the tags in GTM. You could create a form on site with opt-outs for specific tags, however, that data needs to be stored per customer and delivered to the dataLayer to perform tag specific opt-out functionality.
Can you expand on the logic you are attempting to reference from the site?

Google Tag Manager and property per language in Analytics

I have a multilanguage site and my plan is to track every language apart.
Example:
domain.com
domain.com/en/
domain.com/de/
domain.com/fr/
For now, I have one GA Property and one GTM Container for the whole site. I was thinking of creating multiple Properties in GA for every language and one for all together. So that would look like this:
domain.com -> GA-1
domain.com/en/ -> GA2
domain.com/de/ -> GA3
domain.com/fr/ -> GA4
Now in GTM I would add to the container one Tag for each Property and create a trigger for each property. GA-1 trigger fires on all pages, GA-2 trigger fires on some page views that page path contains /en/, GA-3 trigger fires on some page views that page path contains /de/...
Is this the best practice and would this work like this? or is there another method to achieve this goal? Or is it enough to create some Views in GA and create some filters for each language?
The goal is to track every language in a different property so the starting point is always the language. Maybe there are also different conversions in the properties.
In this situation, the best practice is to only have a single GA property that tracks the whole site. If you then want to only report on the data from a specific section of your site (i.e. a specific language) then you would use GA views (and filters) to achieve this. You would filter based on the Page dimension (e.g. Page starts with /fr).
There is no benefit to creating a property for each language except maybe to prevent you from going above the 10 million hits per property per month limit of the free version of GA. On the other hand, there is a benefit to tracking the entire site in a single property. The main benefit is that it allows you to perform cross-language analysis.
I would like to add another solution to your question, where you can actually get the result you're asking for (even though it's not best practice).
If you go to Variables > User-Defined Variables -> New
Let's give the variable a name like UA - Tracking ID
You can choose variable type RegEx Table
Set the Input Variable to {{Page Path}}
And set the rows you need to the following:
Pattern
^/en/.*
^/de/.*
^/fr/.*
Output
GA-2
GA-3
GA-4
Under Advanced Settings
Check Ignore Case, and uncheck Full Matches Only and Enable Capture Groups...
Now add this variable {{UA - Tracking ID}} in your Universal Analytics tag under Tracking ID.
This will work on the tracking for each language.
When that's done, I will just add an extra Universal Analytics under Tags that tracks all the pages with the tracking ID GA-1.
Now two tags will be fired on eg. domain.com/en/, one for the combined domain GA-1 and one for the separate domain, in this example GA-2.

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.

Resources