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.
Related
When using the Google Tag Manager, is it possible to track some things the old way in addition to using the GTM?
For example, I use GTM in order to fire a page view.
However, I want to fire another page view, when a user clicks a button, also known as a virtual page view.
The button in question doesn't have an ID and I don't trust the othet agency, which handles these buttons to consistently keep the same IDs for these elements. So I would rather have them be responsible for these types of page views.
The code for the virtual page view would look something like that:
ga('send', {
hitType: 'pageview',
page: 'button2'
});
Since the tracker is already initialized by GTM, I would only have this code outside GTM.
Would this work if all other google analytics related things run over gtm and where should I put this code in this case? Somewhere after the GTM code on the page I'd imagine?
Google Tag Manager (GTM) by default uses a random name for each tracker, generated for each Universal Analytics tag. There is a possibility to use fixed name for trackers, which is highly discouraged. This means, that you might have difficulties to identify the proper tracker to use, when sending your additional pageview data.
There are however other methods to send virtual pageviews using GTM, where you can benefit from your existing Analytics settings, defined in Google Tag Manager. (Preferably by using Google Analyitcs Settings variable.)
As far as I understand, you have control over the code, to run some JavaScript on the relevant click event.
So instead of directly invoking the ga object, you can send the desired data to GTM, with a call like this:
dataLayer.push({
event : 'virtualPageView',
virtualPagePath : 'button2'
});
Obviously, there are a couple of things you need to set up in GTM, which will be able to act on this event, and send the pageview to Google Analytics.
Create a variable that points to virtualPagePath dataLayer variable, so the newly pushed value could be reused
Create a custom event trigger, that can be used with one or more tags. The event name should match your given event name, virtualPageView in my example.
You need an Universal Analytics tag, which will send the pageview. This tag should be fired by your new custom event trigger, and should have an extra setting compared to your regular pageview tag. Namely, page variable within the Fields to set block should point to the newly created dataLayer variable, that contains your virtual page path.
This way, Google Tag Manager will take care of creating the tracker for you, sending the hit to Google Analytics, and using the virtual page path variable provided by you, instead of the URL in the browser address bar.
I am a developer in codiva.io a java ide for students. I am using codemirror editor.
I want to track on edits (specifically a pause or timeout after last edit).
I had previously used Google analytics, and for events, we will do ga.send(). Now I'm using Google tag manager, for clicks and other events it seems we can configure using tag manager ui itself.
With tag manager, I'm not able to find how implement tracking for this.
Codemirror generates an on change event. On each edit, clear any previous timer and setup a timeout trigger to run after 200ms. (The compilation will be dive at this point, and I want to make sure to track the number of times this event happened)
If I understand you correctly:
Use you change event to have a custom event pushed to the dataLayer:
dataLayer.push({event:'compile'});
Then create a trigger of the type "custom event", set event name to "compile" and use that to fire a Google Analytics event tracking tag (if you use the GA tag template you can select the hit type via a dropdown).
The "push" method of the dataLayer ist not the native array method but a special implementation by the GTM code; GTM uses this to monitor changes to the dataLayer, and the "event" keyword tells GTM to update its internal datastructure so new values become available to tags and triggers.
With the same push you can also pass data to be used as eventCategory etc:
dataLayer.push({
event:'compile',
eventCategory:'myCategory',
eventAction:'myAction',
....
});
You then create new variables of the "dataLayer" type and enter the name of the key you want to access. You can then use the variable in your Ga tag, either by selecting it from the autosuggest list (if you click the icon right to the eventCategory etc. fields) or by typing out the variable name with curly brackets, i.e {{myVariableName}}.
Also keep in mind that the "event" keyword in the dataLayer does not relate to Google Analytics events, except in the sense that it can be used to trigger event tracking (as well as any other tag). "Event" is just a keyword in GTM to indicate that data is updated and tags can now access the new data.
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?
Current tracking of the checkout funnels is done through Pageview using virtual pageviews via
_gaq.push(['_trackPageview', '/checkout/login']);
_gaq.push(['_trackPageview', '/checkout/address']);
which has been hardcode into the webpage.
We are now updating to UA through GTM with enhanced Eccomerce. I would still like to continue to track the checkout funnels with the current method while implementing the Enhanced Eccomerce step 1 / step 2 codes.
My question is If i can fire a datalayer push to overwrite the url which the {{url path}} macro in GTM will track so instead of using /pws/secure/CheckOut.ice?&checkout=true&secure_from=checkout as a goal URL I can use the virtual urls "/checkout/login" and "/checkout/address".
*for some reason only the login and address pages of the checkout are on the same URL.
Would something like this work
dataLayer.push({
'url path':'/blah/blah'
});
If not is there a way I can use the lookup macros in GTM to do Document Path = {{Virtual URL}} (IF it not empty) or {{url path}}. Basically I dont want to have to create multiple tracking codes for each of the checkout funnels with the virtualURL in the Document path. Not to mention how I would get GTM to recongise that one second the URL is a login page and the next it an address page.
Any ideas would be really appreciated
That's correct, you can use the data layer to push your own URL path to Google Tag manager. First you need to set the path in the data layer as you mentioned.
dataLayer.push({
'url':'/foo/bar'
});
Then, in Google Tag Manager, you can do the following :
Go to the tag that handles your GA "page views"
In "More settings", look for "Fields to set"
In the field name, pick {{url}}
For the value, you should create a macro that gets your "url" from the data layer:
As I understand, you can use basic virtual page view behavior by creating GA tag with rewrited url. This tag will be fired, when two events will happen - one for every page view that you want to track. You will only need to add two pushes to dataLayer for events - event itself and dataLayer veritable with url. For example, it can be something like that: dataLayer.push ({'event':'first-event','custom-url':'/my/custom/url1'}).
In GTM you should create a macro, that gets the value of 'custom-url' dataLayer veritable, and use it to rewrite url in your GA tag.
I have a page loaded in via AJAX.
In Google Analytics I could have just called gaq.push('_trackPageView','/someurl') in my AJAX success function. However I can't seem to find a way to do this when implementing GA via Google Tag Manager.
The only alternative seems to be a "virtual page view" event, which isn't exactly what I'm after:
Does anyone know how to do this?
on the Ajax success, do:
dataLayer.push({
'event':'sendVirtualPageview',
'vpv':'/blah/blah'
});
The 'event' is just to send the virtual pageview to GTM. It's not a traditional, although it could be, Google Analytics event.
You should be able to folllow the rest of the tutorial to create your rule and setup the tag type.
Here's how you enable Virtual Page Views on your Google Analytics tag in GTM.
GTM v2:
Go to the Variables page. Enable the Event variable {{event}} by ticking the Event checkbox inside Utilities under the Enable Built-in Variables heading.
Create a variable named Virtual Url with type Data Layer Variable and set the Data Layer Variable Name to "virtualUrl".
Go to the Triggers page. Create a trigger named Virtual Page View with type Custom Event and set the Event name to "virtualPageView".
Create a tag GA Virtual Page Views with product Google Analytics Universal. Set your Tracking ID UA-xxxxxx-xx. Set the track type to Pageview.
Under More Settings > Fields to set, click + Add Field.
Set Field Name to page with value {{Virtual Url}}.
You can add additional fields here, such as title, UTM campaign etc. Add variables then add them here.
Click Continue. Under Fire On, click More. In the triggers popup, tick Virtual Page View. Click Save.
Click Create Tag.
GTM v1:
Create a macro {{Virtual Url]} referring to dataLayer variable "virtualUrl".
Create a {{Virtual Page View}} rule as {{event}} equals "virtualPageView".
You should already have an {{event}} macro, but if you don't, create one using the dataLayer variable "event".
Updates to your Google Analytics tag:
More Settings > Basic Config, tick "Virtual Page Path" and use macro {{Virtual URL}}.
Remove the {{All Pages}} firing rule.
Add the {{Virtual Page View}} firing rule.
GA Fields:
page = the path portion of a URI. eg. /blog/topic/post.html
title = used to specify the <title> contents
campaignMedium = used to specify the URI utm_medium
campaignSource = used to specify the URI utm_source
campaignTerm = used to specify the URI utm_term
campaignContent = used to specify the URI utm_content
campaignName = used to specify the URI utm_campaign
Usage:
In your web app, call it like so:
dataLayer.push({
'event': 'virtualPageView',
'virtualUrl': '/relative/page/path',
'virtualTitle': 'My page title'
});
You can rename any of the variables above - this is just an example based on how we do it.