Google analytics client id cookie not set - google-analytics

First of all, this is first time I'm trying to setup Google Analytics and Tag Manager and need client id from _ga cookie.
I thought that including snippet gtm.js is enough but _ga is not set after that
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?
id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXX');</script>
<!-- End Google Tag Manager -->
So my question do I need to include also snippet for analitycs.js or gtm.js is enough but I'm doing something wrong ?

To summarize you need to follow these steps to start tracking to Google Analytics via GTM (meaning to se _ga cookie created):
implement GTM to page
create in the GTM management interface pageview GA Tag with desired tracking ID UA-XXXX
assign trigger to this Tag - probably All Pages which is automatically created
preview the tag - to see if its working and you are getting real-time hits on your page in Google Analytics interface
publish the changes to the GTM container
After these steps everything should work fine and the cookie should be created.
In case of need refer to this video:
https://www.youtube.com/watch?v=28d60ejfk3s

First create variable on GTM type 1st-party cookie
Variable - GA-COOKIE
1st-party cookie
On Cookie Name = _ga
Obtain ClientID from the Cookie
Create one custom javascript an put this function
function() {
var id = {{GA-COOKIE}};
var aux;
aux = id.split(".");
return aux[2]+"."+aux[3];
}
This function return the clientID

Related

Google Consent Mode from GTM

I am implementing Google Analytics with Google Consent Mode.It works with these two scripts. It will actually track without setting cookies, as meant to.
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag("consent", "default", {
ad_storage: "denied",
analytics_storage: "denied",
wait_for_update: 500
});
gtag('set', 'url_passthrough', true);
</script>
And
<script async src="https://www.googletagmanager.com/gtag/js?id=MYCODE"></script>
<script>
gtag('js', new Date());
gtag('config', 'MYCODE');
</script>
But if I remove the last section and track through GTM instead, it does not work.It only tracks if I change 'denied' to 'granted'.
Anyone knows how to implement Analytics and Consent Mode through GTM?
Here is my advice.
Create 2 variables that can read the status of your consent cookie. The goal is to have them return a value of granted or denied for the marketing and statistics value of your cookie. You can do this through a lookup table or a custom javascript variable, depends on what you like to work with. For instance, Cookiebot has their own variable in the Template Gallery which you can add to your workspace and it will return a value of |preferences|marketing|statistics| depending on what level of consent is given by a user. It does this by reading their own cookie and parsing it into an easy pipe-separated format.
Once you have added the Cookiebot Consent State variable, go ahead and create a lookup table (regex type) variable. In it, add the recently created Cookiebot Consent State variable as the Input Variable. Add a row and type in marketing for Pattern and add granted as the Output value. Enable Set Default Value and add denied.
View example Tag setup image After you are done, copy the tag and make one for statistics with the same values.
Now, Install Simo Ahava's Google Consent Mode tag template from within
the Template Gallery (gif explaining where to find it:
https://imgur.com/kPlFuCG)
Keep the Consent Command dropdown value on 'Default'
Now set the Analytics (corresponds with analytics_storage) and Ads (corresponds with
ad_storage) values to the respective lookup table (type regex) variables we just created. View example row settings image
It is advised to have the default consent tag fire as soon as possible on a page, so
set the tag firing priority to 1 https://imgur.com/FAxrgwO
Add an All Pages trigger to the tag and then save it.
Now, we need to setup the Update tag for Google Consent Mode. This will allow you to update the settings whenever a user clicks on a consent banner. If you use Cookiebot, the click on the banner will generate events based on the type of consent given.
Create a Custom Event trigger, fill in the default Cookiebot event values (cookieconsent_marketing|cookieconsent_statistics) and enable the option Use regex matching, then save the trigger.
Finally, copy the original Google Consent Mode tag we just created. In the Consent Command dropdown, select Update. Check that the correct lookup table variables are set for Advertising and Analytics Google Consent Mode Update settings image and assign it the Custom Event trigger you just created.
Don't forget to update all your Google tags. If you have triggers setup that use a conditional check on cookie consent, you can remove these. Eg. If you have a pageview tag for Google Analytics that has a trigger for All Page where Consent level is XYZ, then remove the consent part from the trigger and just add a regular All Pages. Same goes for any custom event triggers you might use for ie. click tracking (generic events in GA). Keep in mind, this solution only works for Google tags, so make sure you still apply consent conditions in the triggers of tags like those for Facebook, LinkedIn, Twitter etc. If you are unsure about anything, check with a legal advisor.
Test & publish, or ask me any questions you might have.
To manage it from GTM you will find the tags dedicated to consent mode among the custom templates.

Push data to dataLayer and send it to Google Analytics at the same time in Google Tag Manager

I have a complicated issue with GTM, dataLayer and GA.
My setup is:
GA tag is triggered in PageView with Enhanced Ecommerce enabled by using dataLayer
A Custom HTML tag which pushes an item to dataLayer, and it's configured to run just before the GA tag (by using tag sequencing)
Problem is; GA is not including dataLayer changes made by custom Tag even if it's fired after Custom tag.
I noticed, dataLayer.push(...) creates a new GTM event named message.
How can I handle this, GA pageview tag must have the final dataLayer.
tHanks
The datalayer is not re-evaluated in a tag sequence. However you can manipulate the dataLayer directly.
First you need to make sure that the built-in "Container ID" variable is active (or you pass in the ID manually).
Then you get a reference to your GTM instance and call a dataLayer.set with a key and value:
var gtm = google_tag_manager[{{Container ID}}];
gtm.dataLayer.set('someKey',"someValue")
Unlike a dataLayer.push this will be available in the next step in the sequence.

Migrating from GA to GTM - which tracking codes are needed?

I'm not sure which tracking codes are needed to get both Google Analytics (GA) and Google Tag Manager (GTM) to work. I'm currently using the following script with GA ->
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-xxx-x');
</script>
Now that I'm setting up GTM, it is asking me to add the following tracking codes as well ->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s) .
[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?
id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-xxx');</script>
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-xxx"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
Do I need all these snippets? It would be important to be able to both set up event tracking and not lose any previous GA data.
Technically you are migrating from gtag.js to GTM (GTM is a tag manager, and while it can be used to deploy GA by itself it tracks nothing).
If you want to integrate Google Analytics via GTM then the second snippet in your post is the replacement for the first, not an addition. You need the second snippet, then you configure GTM to deploy Google Analytics to your page. For most use cases you do not even need the noscript tag (unless you specifically configure tags that run in noscript mode).
For you debounce-event you can use a timeout to push an event to the datalayer and then use it to trigger a Google Analytics Event. You can do this either somewhere in your page code, or in a custom HTML tag that is triggered on a Pageview (or DOM ready) event.
setTimeout(dataLayer.push({'event':'deBounce'}),20000);
Then you create a trigger of the "Custom Event" type an in the field "event name" you enter "debounce" (without the quotes). You then use that trigger to fire a Google Analytics tag that is set to event tracking in GTM (you notice already that this is not really simpler than gtag.js, but it allows you to control also non-Google tags so it's probably worth the effort).
The "Event" key is special in the dataLayer object - Google overwrites the native push-method in the datalayer array to listen for object keys called "event". Whenever it hits an "event" key it updates all internal variable from the dataLayer (this adds all newly pushed values) and then allows to fire tag.
Yes you would need both the script to set up GTM. 1st code preferably in the head section and 2nd code in the body section of your website.The code enables tag manger to fire tags by inserting gtm.js in the page.
Once your GTM is set up ,for tracking you can create universal Analytics tag to track your events.Check out the url below for tracking events using GTM.
https://www.gravitatedesign.com/blog/google-tag-manager-analytics-event-tracking-2018/

Google Analytics and Tag Manager, support for custom page paths?

Just starting with Google Tag Manager. In my analytics setup I am sending custom page paths to canonicalize or "unify" the page path since in the web app there can be many different URLs for the same page. Example:
/?action=view
/view
/view_page
and in my GA for that page I'm sending:
ga('send', 'pageview', '/view');
I can't figure out how to set this up with GTM. I have access to the server side so I can make any necessary changes there.
I tried setting it in the dataLayer link this:
<script>
dataLayer = [{
'pageview': '/view'
}];
</script>
And I can see the data in the datalayer debug/preview, but it doesn't appear this is actually being set as the page path. I am guessing something needs to be set up on the GTM admin to map the dataLayer value?
If you want to push the pagepath as a dataLayer parameter, then you would need to
configure a dataLayer type variable with the value of pageview (this is how the value you've defined in the dataLayer gets passed into GTM).
In your base pageview tag, you would need to set the page field (under "Fields to Set") with the value of the name of the dataLayer type variable.
It's worth mentioning that your dataLayer declaration MUST come before the GTM container. In this way, you can set your pageview to fire on the regular Page View trigger.

How do I track Virtual Pageviews in the new version of GTM?

I've attempted to use the new version of Google Tag Manager to track virtual pageviews and send those to Google Analytics, but they don't seem to be sending properly.
For my Trigger, I've used the builtin History Change event type and have placed that in a tag with my Google Analytics id. The tag is set to Page View track type.
In Google Analytics, I see a pageview, but it just says the user is on the root directory. Also, I'm working in Localhost.
Is there something I missed?
I'm not sure to get the complete description of your setup but maybe an other way of tracking your virtual pageview could be directly by adding data layers directly in your code.
dataLayer.push({
'event':'VirtualPageview',
'virtualPageURL':'/order/step1',
'virtualPageTitle' : 'Order Step 1 – Contact Information'
});
For the trigger, you will then be able to send the fire on of your virtual pageview as a event matches VirtualPageview.
Your Tag need to be set up as a pageview.
Also, using this article for GTM v1 : http://www.lunametrics.com/blog/2014/09/10/fire-virtual-pageview-google-tag-manager/, you can set up your {{virtualPageURL}}
In GTM V2, in your tag, you will have to use the - More settings / Field to set field Name = Page, Value = {{virtualPageURL}}.
Are we talking here about a one page iOS/Android App?

Resources