See screenshot below. In Google Tag Manager, the Web Property ID is set.
However, when executing _gaq.push(['_trackEvent', …]), then according to
Google Analytics Debugger, it is not available:
Account ID: UA-XXXXX-X
Only when setting the Web Property ID via JavaScript, then _trackEvent works:
_gaq.push(['_setAccount', 'UA-12345678-1']);
_gaq.push(['_trackEvent', 'registration', 'social', user.id.toString()]);
Is it really necessary to set the Web Property ID again in JavaScript? How do I find out more?
Instead of adding events manually and trying to get to work with GTM (Google tag manager). You can actually track events automatically with GTM. Below is a link to that:
http://www.lunametrics.com/blog/2013/10/03/google-tag-manager-auto-event-tracking/#sr=g&m=o&cp=or&ct=-tmc&st=(opu%20qspwjefe)&ts=1391111362
Other than that, instead of adding the Google Id through the default Google Analytics Tag that is created for you. Just create a custom javascript tag and paste your google analytics code in their and make sure it fires on all pages. That should do it as well!
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'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/
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.
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?
I have two Google Analytics profiles in my account. One, for my production account with property ID: UA-XXXXXXXX-1. My second profile is for my staging account to test and it has property ID: UA_XXXXXXXX-2.
In tag manager I have my "-1" tag firing if my url includes "mydomain.com" and my "-2" tag firing if url includes "mydomain.it".
The firing of the GA tag with this setup works fine. I have my staging data separate from production and am able to test basic GA pageviews.
What now need is the ability to separate events. Currently, I call setAccount prior to each event tracking (I know this can be in single call).
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_gaq.push(['_trackEvent', 'Videos', 'Play', 'Gone With the Wind']);
What I'd like to do is call the _setAccount method and dynamically pass in the value from Google Tag Manager where I set the property ID depending on the url.
Is there a way to do something like this (or something better) so I can set the account based on a Tag Manager rule?
_gaq.push(['_setAccount', [Tag Manager Property ID] ]);
I appreciate any advice/help you have.
Thank you!
IMO passing data from the tag manager to the website, apart from all practical difficulties, would totally defeat the purpose of having asychronous loading code.
You "push" methods on a stack so you can process them when the code has loaded at some undetermined point in the future. If you want to take the account id from the code you'd have to wait until it's loaded, so you'd loose all advantages from asynchronous loading.
As far as I can tell (haven't used tag manager extensively yet) the correct way would be to push the event data to the "data layer" variable :
dataLayer.push({'myevent': 'mylabel'});
and use the variable (like dataLayer.myevent) in the code deployed via the tag manager (so you do the actual event tracking in the domain specific code).
This is described at: https://developers.google.com/tag-manager/devguide
There is actually an event tracking template for Google Analytics in the tag manager. For the event value you can create a new macro and set it to a dataLayer variable. Then add your domain specific rule and you should be all set.