Access GTM google tag manager variables from outside of GTM? - google-tag-manager

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?

Related

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.

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

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.

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.

Google Tag Manager: gtm.dom and gtm.load are missing

I'm implementing the Google Tag Manager script on a clients website.
I'm encountering a random issue: the dataLayer doesn't get its gtm.dom and gtm.load objects.
It only has two Objects, one with a bunch of variables which I'm passing and a dataLoaded event which I'm pushing in.
The issue does appear randomly on different pages, sometimes the dataLayer is correctly filled, sometimes not.
What could be a reason for not getting those two gtm objects?
(note: I'm the one who implements the javascript. The GTM configuration is done by an external contractor)
I found a solution here:
https://productforums.google.com/forum/#!searchin/tag-manager/gtm.load/tag-manager/0Lpevpf2Fss/OMJFuaM04BsJ
I didnt push my variables but assigned them to dataLayer. Its overwritten, so the gtm objects are missing.
The best implementation is assign (not push) variables before tag manager call script.
If you want to use dataLayer.push method, firstly load google tag manager script on the page and
call dataLayer.push in javascript timeout.

Can I set GA custom variables anywhere in the page?

I'm just getting started with splittesting using Google Analytics (GA). Rather then using different URLs for different versions of a page I want to use the same URL and differentiate the versions via GA custom variables.
I've managed to accomplish this in my Django project with a conditional in the view-layer that loads up different html templates for different versions and the html-templates also take care of setting the custom variables.
However I think that it would be a lot cleaner to have all of my splittest code in a javscript file that:
1) Sets/reads a cookie on the browser specifying which testing bin this browser is in.
2) Modifies the page html and css to create the different versions
3) Records the version in a GA custom variable
Right now I'm setting all of my custom variables right in the code snippet that I've copy-pasted from GA.
My question is simply: Can I set the custom variables later in the page buy a _gaq.push(), or do they need to happen before the anonymous function that ends the snippet from GA?
Custom variable data gets recorded when GA makes a tracking GIF request, which occurs with _trackPageview or _trackEvent.
So, you can set the custom variables anywhere in the page, as long as there's a _trackPageview or _trackEvent somewhere afterwards.

Resources