Event_name parameter in gtag.js - google-analytics

I am trying to migrate from Universal Anaylics to the newer Global Site Tag. I wish to migrate this code:
ga('send', 'event', 'MyCategory', 'MyAction', 'MyLabel', 1);
to:
gtag('event', '[EVENT_NAME???]', {'event_category':'MyCategory', 'event_label': 'MyLabel','event_action': 'MyAction','value':1});
I read https://developers.google.com/analytics/devguides/collection/gtagjs/events#recommended-events but I do not understand what the value for the event_name parameter should be and what the purpose of this value/parameter is.

The event name in gtag defaults to the event action in GA. If you use the "recommended event names" as provided by Google the gtag code will automatically insert category and label with predefined (by Google) values (which you may override). You can however also use custom event names (so you need only specify category and label in the JSON).
In the larger scheme of things this is Google's attempt to unify web tracking and mobile tracking, since the suggested events for gtag.js match the recommended events for Firebase mobile tracking.

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.

Execute Google Analytics Functions in addition to Google Tag Manager

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.

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.

Can I use same GTM tag to fire for multiple GA accounts?

I am working on one project which needs to fire same GTM tag to for two different GA account is this possible? or should I have duplicate all the same tags for two GA accounts?
There is no standard way to fire multiple properties in one tag. However I can think of two possible workarounds.
(Updated: The same questions came up on the GTM forum and naturally Simo Ahava had a solution).
One would be to create the tracker object manually in a custom html tag (the actual pageviews happen still via Ga tag templates). This would allow you to use Google Analytics plugins and there are plugins (e.g. here or here) that send hits to multiple properties.
The other workaround is to use a feature called tag sequencing. This has the advantage that you do it with "pure" GTM instead of GA plugins. I will first show the steps and explain what happens later.
The Setup
First you need to set up your tracking id as a variable of the datalayer type. As default value you set your first tracking id:
Next you create a custom html tag that will later set the second tracker id. This also sets a custom event necessary to fire the second tracker. Note that there are no triggers attached to this tag, and that it is set to "fire only once per page" (the last thing is very important! You can set this in Advanced Settings -> Tag firing options).
Then you create a trigger that uses the custom event from the previous step:
Now comes the magic with the GA tag. You use the variable for the tracking id that you have created earlier. Then you go to "Advanced Settings", expand the "Tag Sequencing" options and check the mark before "fire a tag after fires". You select the custom html tag from above (named set2ndId in my example). Then you attach a pageview trigger and the custom event trigger (called 2ndTracker in my example).
The sequence
GTM is loaded and evaluates the tracker id from the default value of the datalayer variable
The pageview trigger fires the GA tag
The tag sequencing setting fires the custom html tag that sets the datalayer variables for event and tracking ind
Since we have an event the datalayer is re-evaluated, and the new value with the second tracker id from the custom html tag is set
The custom event trigger fires the GA tag with the second tracker id
Since the custom html tag was set to "fire once per page" it is not fired again (else you would end up in a loop)
A word of caution: This is somewhat clever, even if I say so myself, but at the moment it is a bit "proof of concept". I use it without problems for pageview tracking, but I'm still working the kinks out for event tracking etc, so you have to decide for yourself if this is useful for you.
But at least it answers your question: yes, it is possible to send to multiple properties without duplicating the GA tags, but it takes some additional setup so you have to decide if this actually saves you work.
yes it is fine to fire multiple GA properties in single GTM.
Maybe this tutorial will help you:
http://www.kristaseiden.com/step-by-step-adding-a-second-ga-property-via-google-tag-manager/
I hope this helps.

Google tag manager custom event tracking

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.

Resources