multiple trackpageview calls in code - google-analytics

i have many database generated pages having different url's. all these are currently tracked by calling
_gaq.push(['_trackPageview']);
I want to now track these pages as one but I can't remove the existing _trackPageview call. So I decide to add a new call so that I'll have two calls to _trackPageview, as:
_gaq.push(['_trackPageview']);
...
_gaq.push(['_trackPageview',static_url]);
will this work. will this track two pageviews for the same page?

Yes, your current implementation should double count the pageviews for each page, which probably isn't what you want.
If you can't get rid of the first call, I suggest creating a new separate profile and sending the second page call to this new profile so at least it's tracked independently. The best way to do this could be through adding a prefix on the second tracker:
_gaq.push(['_trackPageview']);
...
_gaq.push(['b._setAccount', 'UA-XXXXXXXX-2']);
_gaq.push(['b._trackPageview']);
Here's a reference where something similar is discussed: http://productforums.google.com/forum/#!category-topic/analytics/discuss-google-analytics-features-with-other-users/5nDlmeAriIw

If you use Google Tag Manager you can set up firing rules for each page so each page generates a separate firing event. This also means you can manage your tags from within GTM so you don't have to update the tags on each page every time you want to make a change, as long as the GTM container code is on each page. Using filters from within Google Analytics means you don't need different profiles.

Related

Google tag manager resetting events list on page load

We put the GTM code snippet in the site.
However the sequence of events resets every time I switch from one page to another.
For example - if a user comes on the login page, enters the login details and clicks the login button to go to the home page, the list of events reset and I lose all data about previous events.
How can i ensure that I dont lose the event data from previous pages?
There's nothing you can do about the browser resetting window-level variables, that's just how they work.
You might want to reconsider the way you think about GTM and the dataLayer. Instead of thinking of the dataLayer as a list of all past events, think of it as just a way to send data to Google Analytics (or whatever too you're using). Google Analytics is what stores the events, the dataLayer is just an interface for sending them there.
Having said that, here are some potential solutions for remembering values across pages:
You could try using something like window.sessionStorage, which will allow the browser to remember certain values across pages. This isn't a solution for remembering the entire dataLayer, but if you have one or two bits of data you want on multiple pages, this might work.
Alternatively, if you have data that's stored on the server (like the user's ID) that you want to have client-side, you could have your server generate some code to push that data when the page loads, like this:
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'userId': '123'
});
Edit: I just learned that Simo Ahava found a way to do this. It is very complicated and I still recommend changing the way you think about the dataLayer, but it is possible.

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 Behavior when Calling trackPageview Twice

I have a site using Google Analytics. On one page clicking a button changes everything about the page. However, it does not change the page's URL. I would like to track it as a separate page in Google Analytics. So I would assume I would trackPageview("/new") (or really _gaq.push(['_trackPageview',"/new");), however before I implement this I want to make sure events and such are not counted twice.
That is to say, if I were to load the page / (and have the default trackPageview call occur) then call trackPageview("/new") and finally call trackEvent("Test",…) would the Test event be reflected on the /, /new or both?
In your example, 'Test' would be reflected on '/' because that is the window.location.pathname.
There is an undocumented GA function called _set where you can override the variable like the code below:
_gaq.push(['_set','page','/new']);
_gaq.push(['_trackPageview']);
$("#submit-button).click(function () {
_gaq.push(['_trackevent']); // tracks to '/new'
});
After doing some testing it looks as though the Test event shows up on / regardless of pushing a new pageview.

Passing data from Google Tag Manager to Google Analytics

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.

Using Google Analytics, if I explicitly log an event using _trackPageview, does that count as a 'real' page view?

For example, if I have a news page that's already being tracked via GA and I add a javascript event to capture clicks on a specific link to the news page (e.g. navigation) am I then 'double counting'?
If a fake pageview is not beneficial in this situation, and from your description that you're looking to track an engagement click on your page, use Event Tracking instead of Pageviews.
Tracking a click/event is easy (especially if you're already using Javascript). The best part is that this event is not considered a page view, keeping those stats safe.
The implementation is simple and allows for quite a bit of customization:
_trackEvent(category, action, opt_label, opt_value)
Below is an example of a link that's been encoded with an event tag:
Play
Here's the Google Analytics resource page on Event Tagging:
http://code.google.com/apis/analytics/docs/tracking/eventTrackerGuide.html
Well it's not a real page view, but Google Analytics thinks that it is--i.e., it shows up in your pageview counts.
Fortunately, it's easy to filter those so they don't contaminate your pageview data.
So for instance,
_gaq.push(['_trackPageview', 'unique_virtual_pagename']);
So in your GA Browser, you'll see the number of clicks actually shown as the number of pageviews of *unique_virtual_pagename*, which is not good.
There are a two ways to fix this (that i am aware of): (i) set a temporary filter at the bottom of the pageview table; or (ii) set a persistent (c*ustom*) filter in your Admin Panel (which i think is best) to remove pageviews having only the name *unique_virtual_pagename*, or whatever name you've chosen. This will of course take up to 24 hours to set, so the best plan is to set the filter first, then add the javascript to your page. so the filter is active when you begin collecting clicks.
But that just solves the problem of disaggregating these virtual pageviews from your real pageviews, you still need a way to count/record them.
I prefer to create a separate profile in these cases. So first, i filter the virtual pageviews from my actual pageviews using a custom filter, then i create a new profile which has another filter excluding everything but these virtual pageviews. I usually give that profile a name based on the event.
What you're doing is registering what's called a 'virtual page view'. To GA it's seen as a real page view and shows up in your content report's and page view counts etc. This is often useful if you want to show a page view for media that GA can't track. It's also commonly used to count an event, such as a button click, as a goal conversion.
If you just want to record the event and not count a page view you should look at using GA event tracking instead.

Resources