There is a guide that describes how to split the code of Google Analytics.
https://developers.google.com/analytics/devguides/collection/gajs/#SplitSnippet
The idea is as follows:
Declare the object for GA at the head of the HTML document.
Load the JavaScript library at the bottom.
All the measurement data is preserved and sent.
Everything is described in the documentation linked to this question.
You may actually find it useful to speed up load time of your websites
- especially mobile ones.
I would like to know how to do it with the new universal analytics code.
Thanks in advance for any links or answers.
The analytics.js snippet is part of Universal Analytics
Look at the above link. It will clarify your doubt.
Try this code snippet developed by Local Wisdom
http://www.localwisdom.com/blog/2013/09/global-google-analytics-event-tracking-script/
This works for me:
window.ga = window.ga || function(){};
I took it from the original tracking code (https://developers.google.com/analytics/devguides/collection/analyticsjs/) snippet:
(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');
which does basically the same check
i[r] = i[r] || function() {
…
}
i is the window object and r the name for the analytics function name. Both get passed when the function invokes itself
(window,document,'script','//www.google-analytics.com/analytics.js','ga');
In general the analytics code itself is loaded asynchronously anyway, so it shouldn´t hurt to much if you have it in the header.
Update: I found the proper way to load google analytics on localhost while not sending test data in here https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced, for localhost use the following code:
ga('create', 'UA-XXXX-Y', {
'cookieDomain': 'none'
});
(Old: I had to check / define 'ga' because in my local development environment I do not load the analytics code at all and so I would get a reference error.(
Related
I added two different type of google analytics codes to my site. First:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(["_setAccount", "UA-00000000-1"]);
_gaq.push(["_trackPageview"]);
(function () {
var ga = document.createElement("script"); ga.type = "text/javascript"; ga.async = true;
ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";
var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Second:
<script type="text/javascript">
(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', 'https://www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-00000000-1', 'auto');
ga('send', 'pageview');
</script>
when I added these two codes to my site that time my bounce rate is very low, last 30 minutes page view is very high and final report is more page views per day. If i removed these two codes and i added new tracking code means bounce rate is very high, last 30 minutes page view is very low and over all page view also very low. What is happening.? Please help me guys.
Pageview are tracked double if you use the the tracking id (the number that starts with "UA-") for both code versions.
Re. your comment "I couldn't understand the difference between these two codes":
The first set of code is an outdated version of the tracking code. You should not use this anymore, especially since it does not support all current featues of Google Analytics (and will not support features that might be added to GA in the future).
The second set of the is the current version (also called "Universal Analytics), you should use that exclusively. If you still need multiple trackers you need to use named trackers if you want to configure them independently (if you use the same tracking id you will still get double pageviews).
So, use the second version of the code, drop the first one and you should be fine.
Why do you want a pageview twice? That definitly will break google reports because you send useless data.
Pageviews will double (every page is viewed twice).
Bounce rates will decrease drastically (a bounce means a visitor only sees one page = has one pageview and then leaves - but your visitors will have 2 pagesviews, so no bounces)
So only have a tracking code once.
I´m having trouble figuring out how to track only one specific domain for my project in google analytics. I have a website that runs on two different domains (www.x1.com & www.x2.com)
In the following snippet I´m tracking traffic for both websites (that works) and I´m also trying to track only traffic for x2.com (does not work)
(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-XXXXX-1', 'auto');
ga('create', 'UA-XXXXX-2', 'auto', 'x2Tracker');
ga('send', 'pageview');
ga('x2Tracker.send', 'pageview');
I have two different accounts in my Google Analytics website.
How can I track only traffic for my www.x2.com domain (but not both) ?
You can apply a hostname filter to views of a Google Analytics account, so that it rejects traffic from www.x2.com (for example).
Bear in mind though that it believes x2.com and www.x2.com are different hostnames, so you should exclude them both. This is actually why I recommend an exclude filter, because you might have trouble stacking a pair of include filters.
This is done in Admin - View - Filters - New Filter
I am having a self-referral issue in Google Analytics that I cannot seem to solve. Researching the issue, most conclude the main causes are either:
Tracking code is missing from certain pages.
Incorrect cross-domain or subdomain tracking.
I use an include file with the Universal Analytics code on all pages, so I know missing code cannot be the issue. All user traffic is on a single domain so that cannot be the issue either.
I have also read that sessions expiring can cause self-referrals, but around 80% of referrals are coming from my site so I doubt this is the case.
Here is the tracking code in the include file (not that I think it matters, but I'll include it anyway):
(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-XXXXXX-X', 'auto');
ga('require', 'displayfeatures');
ga('send', 'pageview');
The only other suggestion I have found is that absolute links could be causing the issue. I am tempted to say this could be the case, as I have noticed the main pages causing self-referrals use absolute links for navigation.
My content is being distributed all over the web as an iframe, and I wish to monitor on which pages I get my content viewed on.
I created a Google Analytics property to collect this data - even though GA properties are tied to a domain, and in my case there is no specific domain (as said - it's embedded in pages all over the web).
Because I have many pieces of content that I spread around the web, I hijacked the GA page view mechanism and I report customized information under the page view:
as the URI I provide an internal content ID that makes sense to me
as a page title I use my content's title
It 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-XXXXXXXX-XX');
ga('send', 'pageview', {
page: '/<my internal content ID>',
title: '<my internal content name>'
});
</script>
At first it worked brilliantly and I could see for each piece of content - which pages it was viewed on, (under traffic sources in GA - medium=referral, source=hosting page - even for direct browsing... yummy!), but over time I noticed more and more cases in which this information was not available (I got medium=direct, source=not set) - but it only happened in some cases, which I didn't manage to isolate.
Does anyone have an idea of what I'm doing wrong?
I know I'm bending the traditional use-case of GA, but hey - this is what gets us people through the day - right?
I just finished reading about Google Analytics cross-domain linking. All that has done for me is created more questions. I'm hoping to get some help understanding it all.
Our portal site is hosted at http://aq3.processmyquote.com. We create new websites using the first subdirectory as the site name. All secure traffic is directed to https://aq3.processmyquote.com/. For unsecured traffic, we allow our clients to specify an alternate domain name to use for their pages.
We have a single univeral analytics account for the entire site, and views for each client portal. Our issue is that sites which use an alternate domain name are getting tracked as referrals, and we're losing the organic keywords.
Here are some examples: http://www.autoquoter.com, http://www.idriveaffordable.com, http://www.venamex.com
Google's documentation on this states that I should include the autolinker on the main site, and add allowlinker on the secondary sites. How does that work with a web portal? The same tracking code is inserted into each site. Is it ok to just list all the possible domains when creating the tracker?
(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-XXXXXX-20', 'auto');
ga('require', 'linker');
ga('linker:autoLink',['www.autoquoter.com',
'www.idriveaffordable.com',
'www.venamex.com']);
ga('send', 'pageview');
This would be added to all of the sites, since they are all pointing to the same web site. The only difference is that the sites are skinned independently. I'm not sure how I would add the destination ga create code or if I even need to.
ga('create', 'UA-XXXXXX-X', 'auto', {
'allowLinker': true
});
Well, I've figured this one out on my own. I'm adding my answer here to help anyone else who may encounter the same issue. In the scenario above, it's important to consider that the purpose of cross-domain tracking is to pass the analytics cookie to the next web site.
Let's say that a user landed on http://www.autoquoter.com and enters a zip code to start the quote wizard. The action of that form needs to be secure so it uses the secure domain name, https://aq3.processmyquote.com/... (the full url is omitted for brevity).
In order to pass the analytics cookie to this url, the url needs to be modified to append a _ga parameter to the query string. This is what the google autoLinker does. It just needs a little help to know which links in the page to modify.
<script type="text/javascript">
(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-XXXXXX-20', 'auto', { 'allowLinker': true });
ga('require', 'linker');
ga('linker:autoLink',['aq3.processmyquote.com']);
ga('send', 'pageview');
</script>
Notice that we also can include { 'allowLinker': true }, so that the current page would process an incoming _ga parameter if it exists. On each page, I include the domain names of all possible links in that page. So, if I'm on a secure section of the site, I can only go back to the domain name for that client's portal. In this example, that would be autoquoter's domain name. While we could add all the possible domain names, it's not needed. You only need to include the names that would occur in actual links on that page.
ga('linker:autoLink',['www.autoquoter.com']);
I hope that helps someone. If you have any questions about this, add a comment and I'll try to help.