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.
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 trying to integrate Google Tag Manager with my Ember app. I'm having a hard time understanding how to notify GTM that the page changed, and send a page view event.
I've read a lot of things online, mostly working around creating a new variable for a "virtual page" or something, but obviously this is far from ideal.
I'd like to be able to just use dataLayer.push to notify the page actually changed. Is there an easy way out I didn't find or is it really a pain to track with GTM on SPA?
This was asked quite a while ago. With the current feature set of GTM, you can easily set up SPA page tracking without much trouble.
First, go to Triggers and create a new trigger. Select History Change as the trigger type. This will create a trigger that fires every time the location history changes.
Then create a new Tag of Universal Analytics type and set it up as per the screenshot below.
[
As for the trigger, set the previously defined History Change trigger, and you're done. Every time a navigation occurs in your SPA, a page view event with the proper page path will be triggered.
EDIT: as trognaders pointed out in a comment, this does not track the initial page view. To remedy, simply add an additional trigger for your tag that fires on the Page View event (All Pages). See screenshot below.
You definitely need to push events into the dataLayer which you can then trigger a GA page view tag in GTM. So to push an event into the DL:
dataLayer.push({'event':'virtualPageView'});
Then setup a trigger called 'vpv' which fires on a custom event called 'virtualPageView'. Add that trigger to a GA tag.
The best thing to do is also send through the details of the virtual page when you send the event. This way you can set up variables that pull these dataLayer property values into the page view call. So you might do this:
dataLayer.push({
'event':'virtualPageView',
'page':{
'title':'contact us',
'url':'/contact'
}
});
So you'd setup a variable called 'DL- page title' (for example) which is a dataLayer value of 'page.title' and another variable called 'DL - page url' which is a dataLayer value of 'page.url'.
You then setup a new Universal Analytics tag which has all your usual pageview settings but with 2 'Fields to Set' (under More Settings). Set 'title' to {{DL-page title}} and 'page' to {{DL - page url}}
Finally set the trigger to 'vpv' and you'll find everytime you push the event + data into the datalayer you'll get a pageView fired off with your virtual page's title and virtual url.
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.
I am currently working on a site that uses hashsignaling for page loads. This means there are very few page "loads", but rather lots of ajax content changes and template manipulation.
My question is: how do I track this using Google Analytics? Is there a way I can trigger a GA call in my code that captures the entire url including the hash and any other parameters?
It depends how you want to represent your site activity in GA, but you could decide what you want to qualify as a "pageview" and call the _trackPageview() method with URL included as a parameter any time that activity happens:
_gaq.push(['_trackPageview', 'YOUR URL HERE']);
Hashsignaling fire a hashsignal.hashchange event on window object when a page is updated :
$(window).trigger('hashsignal.hashchange', [subhash]);
So you can bind a Google Analytics tracking call to this event with something like :
$(window).on('hashsignal.hashchange', function(event, subhash){
_gaq.push(['_trackPageview', subhash]);
});
I am using the async version of Google Analytics along with custom variables and events. I have placed the tracking script (cut and paste from google) at the end of the section.
The custom variable is set above the tracking code in the head section. I'm assuming that Googles Analytics will send the data including the custom variable on page load. What happens if I have onclick triggered events? Will the custom variable in the head section get sent along with each event that is triggered as well?
I'm not sure how this plays out with javascript.
Thanks!
It depends on the scope of the custom variable. Page view scope will only send/report the first time. You will have to include it in the onclick code that is triggered to get it to count each click.
If you have it as a visit/session scope, in the reports, it will associate itself for each click event for the duration of the visit/session.