I have new analytics.js on a new webpage. This webpage has to be linked analytically to the existing webpages, managed by other people.
They sent me a line of code to do it:
_gaq.push(['_link', $(this).attr('href')]);
This is the old version of the analytics. I understand, that the same thing in new analytics can be accomplished using:
ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);
To where on the second I should put '_link' and $(this).attr('href') parameters?
There is not much documentation on the Google website. I have enabled official linker plugin, but they say, that this is not enough.
Edit: Bit of js after loading analytics.js looks like this:
ga('create', 'UA-12312312-1', 'auto', {'allowLinker': true});
ga('require', 'linker');
ga('linker:autoLink', ['friendlydomain.com'] );
ga('set', {page: window.location.href});
ga('send', 'pageview');
Where UA-12312312-1 is their tracking id. I can see, that ga parameter is added to the links going towards friendlydomain.com
This line of code:
_gaq.push(['_link', $(this).attr('href')]);
is not for outbound link tracking, it's the linker for Cross Domain tracking (see the documentation for the old ga.js library).
To accomplish the same with analytics.js it would be best to use the autolink plugin. This appends the client id to outgoing links; the linked pages must have linking enabled in the configuration to receive the parameter from the url and set it as client id when the tracker is created. GA will then use the client id to merge the sessions on both domains into one.
This will work only if both pages run analytics.js. Also you need to set both domains in the referral exclusion list in the property settings.
analytics.js is fairly flexible, so it mostly depends on how you want to view the data on the GA site.
One possible solution would be...
ga('send', 'event', 'link', 'click', $(this).attr('href'));
Also, the docs are fairly comprehensive. You might consider re-reading them here: https://developers.google.com/analytics/devguides/collection/analyticsjs/events#event_fields
Related
I made the caching of my web app much more aggressive and essentially made it a single page web app.
Even though I thought I implemented Google Analytics correctly according to the documentation, I am getting lots of "not set" pages. So I am guessing something is wrong with my code.
Here's how I believe I send a pageview:
ga('send', {
hitType: 'pageview',
page: pagePath
});
Where pagePathcontains a synthetic page address.
What is wrong about this?
for the single page web application you should have to apply the GA where the change of Route or hash occurs.
And there you should have to include following line of code in without removing the previously written code on the one time loaded page:
window.ga('send', 'pageview', location.hash);
Try this:
ga('send', 'pageview', 'page path');
There is a alternative method using GTM , where you can easily (!!) set-up virtual pageviews, more info on this link
EDIT:
This is an example of one of my sites where i set the fields for a Virtual Pageview in GTM
for SPA Solution,
you could try this one
ga('set', 'page', '/new-page.html');
ga('send', 'pageview');
https://developers.google.com/analytics/devguides/collection/analyticsjs/single-page-applications
I've a question related to the simple tracking functionalities of Google Analytics.
I've realized my website with reveal.js, so my site follows a step navigation
http://[mywebsite.nl]/#/cover
http://[mywebsite.nl]/#/welkom
http://[mywebsite.nl]/#/pagina1
http://[mywebsite.nl]/#/pagina2
and I setup google analytics to track statistics.
My tracking issue is that when I land on my http://[mywebsite.nl] and I'm redirected to http://[mywebsite.nl]/#/cover it happens that the same page is tracked twice.
Is there a way to remove the tracking of the / of the website?
Thanks
You can take a look at Google's analytics.js tutorial, it allows some customization:
https://developers.google.com/analytics/devguides/collection/analyticsjs/pages
Page - The page path and query string of the page (e.g. /homepage?id=10). This value must start with a / character.
GA is nice to use as is, it provides some valuable insights.
But it is generally more valuable to track not page views but user actions.
GA is about page views, but as soon as you really want to understand users behaviour you need to track actions. I would recommend to take a look at http://www.devmetrics.io or http://mixpanel.com analytics.
You specify event and its properties. That means that you have full control.
var pageName = document.location.pathname;
// custom pageName processing if you need
devmetrics.userEvent('page_load', [pageName]);
....
// inside button handler:
devmetrics.userEvent('button_click', [pageName]);
I wouldn't recommend stripping the hostname from your pageviews, that can cause other issues with data quality.
Ideally, you would use a 3xx redirect to redirect from your main page to /#/cover, or don't send a hit before your site redirects. If that's not possible, you could try using a filter to drop hits from the URL you don't want to be double-counted.
You might try Google Tag Assistant Recordings to validate that your flow works as expected.
It was easier than I thought, sometimes we tend to over complicate things..
This is the super easy answer I was looking for, it is necessary a small tune on the javascript code
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
if(window.location.pathname != '/'){
ga('create', 'UA-XXXXXXXXX', 'auto');
ga('send', 'pageview');
}
It is just necessary to check the window.location.pathname and call the analytics function only for the desired urls.
I am first time posting something here...
Issue:
We have moved to Universal Analytics few month back and since then form submission conversion attribution is broken. Almost all conversions (form submission) are attributed to direct traffic. Actually we have our confirmation page on third domain (now.eloqua.com) but it has the same code as we have on the website. But I guess due to different domains, cookies are getting drop in transition from main website to the confirmation page as analytics.js does not set up _utm cookies
Possible Solution, is I guess using Cross Domain Auto Linking (auto link plugin), We have implemented below code on both main website page and Thank you page (eloqua.com).. but issue still persists. Can somebody suggest what wrong I am doing here...
function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-xxxxxxx-X', 'auto',{'allowLinker': true});
ga('require', 'linker');
ga('linker:autoLink', ['eloqua.com']);
ga('send', 'pageview')
As you are saying that your conversions are form submission you might have to add another parameter to the call to the autolinker plugin (this is as per documentation, I have not tested this):
Cross Domain Auto Linking for Forms
Universal Analytics also includes the ability cross domain link
elements automatically. ga('linker:autoLink', ['destination.com',
'dest3.com'], false, true);
Where the third parameter (false) tell the autolinker to not use the anchor/hash and the fourth parameter tells it to auto-decorate form action urls (else only links get decorated), so this is what you need. You should see a _ga parameter appended to the urls on arrival at the other domain if this is working correctly.
Plus I'm reasonably sure that you need to add both domains to your referral exclusion list.
So, we have a couple of wordpress websites. For one of the them the analytics code is of an older version (gaq.push) format.
For this particular website, we have the tracking set up just fine and Google analytics goals are properly working. In the "additional setting" section of contact form 7 (via WP-Admin), we are using:
on_sent_ok: "_gaq.push(['_trackPageview', '/rfp-form-thank-you']);
However, for the other one, the newer version shows up which is ga.js based and the format looks like this:
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-4248211-9', 'www.website.com');
ga('send', 'pageview');*
</script>
Now, the issue is: we tried following the method as we did for the other one but that doesn't seem to work. It may be because the analytics code is different this time.
How can we set up proper form submission event capture so that goals can be fulfilled in Google Analytics?
The method used in the question is in fact a virtual pageview, not an event. The correct syntax is as follows for virtual pageviews under the new Universal Analytics library:
ga('send', 'pageview', '/enter-new-pageview-here');
For CF7, you'd use the above syntax and then enter "/enter-new-pageview-here" as a destination URL goal, which is what I believe the qustioner is trying to achieve.
Reference
Syntax is different (and your example is in fact analytics.js based, not ga.js):
ga('send', 'event', 'category', 'action', 'label', value);
Documentation
Currently in my website, I used HTML5's pushState() and popState in links to increase the speed. However, this doesn't really change the real URL and it looks like it will affect and mess up the Google Analytics's code. (doesn't show a url change) Is there a possible solution for this? Thanks,
If you are using the newer analytics.js API, Google's documentation requires the following code to trigger the event:
ga('send', 'pageview', '/some-page');
If you are using the older ga.js API, David Walsh suggests AJAX websites to use the _gaq.push method:
_gaq.push(['_trackPageview', '/some-page']);
I know it's old question but since this question is first result in Google about tracking pushState() in Google Analytics and all answers are wrong I decided to answer it.
In other answers they mentioned to use directly ga('send' ..... ) but this is wrong way to do it.
First you have to 'set' parameters and then use 'send' to track it.
If you want to update only url, use following code
// set new url
ga('set', 'page', '/new-page');
// send it for tracking
ga('send', 'pageview');
If you want to update url and title, add title parameter to it
// set new url and title
ga('set', {
page: '/new-page',
title: 'New Page'
});
// send it for tracking
ga('send', 'pageview');
Source Single Page Application Tracking - Web Tracking (analytics.js)
February 2018 Update - Global Site Tag (gtag.js)
Google Analytics has a new tracking code snippet, so the other answers might not work for gtag.
This is the default tracking code. It only runs once even though we try to run it each URL changes.
gtag('config', 'GA_TRACKING_ID');
But with a page_path parameter we can make GA run manually.
gtag('config', 'GA_TRACKING_ID', {'page_path': '/new-page.html'});
And we can make something like this.
var origin = window.location.protocol + '//' + window.location.host;
var pathname = window.location.href.substr(origin.length);
gtag('config', 'GA_TRACKING_ID', {'page_path': pathname});
Single page application tracking with gtag.js (Google documentation)
Recent answer (2017)
You can now use Google's autotrack.js, a script that enhances analytics.js.
It includes a plugin that automatically tracks URL changes for single page applications.
You just need to include the script and the following line in your html:
ga('require', 'urlChangeTracker');
2020 Update
If you are using Google Analytics 4 you don't need to push the event anymore IF you enabled the Page views option in the Enhanced measurement feature in Data Streams menu.
At the time of writing, here in September 2013,
Google Analytics has a new JavaScript API.
After you've included Google's new "analytics.js" asynchronous snippet, use the send pageview command to track pages:
ga('send','pageview');
After your pushState madness, use this send pageview command to track your asynchronous navigation. According to Google's Documentation on Page Tracking with Analytics.js, the send pageview command will magically read and track the new location given by pushState, as it will, in the moment the send pageview command is called, use the following values by default (though you can specify them):
var locationToTrack = window.location.protocol+'//'
+window.location.hostname
+window.location.pathname
+window.location.search;
var titleToTrack = document.title;
var pathToTrack = location.pathname+location.search;
Note, Google's standard analytics snippet includes an initial send pageview command.
Update:
I've implemented Google Analytics tracking on my website in the way above that I described -- however, it does not seem to be successfully tracking any of the pushState page views.
I'm going to try specifying the location, title, and page name explicitly on the send pageview command, and see if GA tracks properly.
I'll post back with the results, unless I forget.
Using ga('send', 'pageview') was not registering a pageview on GA.
The way that worked for me is:
window.ga.getAll()[0].set('page', location);
window.ga.getAll()[0].send('pageview');
I can use it after changing the url with history.pushState.
I also had problems with ga('send','pageview');, after using history.pushState.
The workaround was simply make the URL explicit.
ga('send','pageview','/my-url')