Google Analytics Simple visitors count - google-analytics

I am new to GA and I have the following scenario:
I have a website that does not require the GA services at all except for one page.
This page's URL something like this: http://example.com/events?eventId=1234
What I try to achieve is to count the visitors for the different events specified in the parameter. (So later I can retrieve this information and I can show how many people visited a particular event's site).
I have been looking at https://www.google.com/analytics for a while however I could not figure out how to make the GA distinguish by the parameters. So far I registered at GA and I have the unique code and I can include this to the page:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxxx-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);
})();
Additionally this web application is implemented with Play framework

You can use event tracking or just log a virtual pageview
e.g.
_gaq.push(['_trackPageview', '/events/{id}']);
Replace the {id} with the actual id

By default, the on-page GA code snippet will record the URL, minus protocol and domain. That means if you have
www.example.com/events?eventId=1234
vs.
www.example.com/events?eventId=4567
You will see 2 separate entries in your pages reports:
/events?eventId=1234
/events?eventId=4567
So out-of-the-box you should be good to go.
However...one issue you brought up in a comment:
does it work even if there are other parameters as well?
Yes it will, and that presents a problem. Because these 2 will also count as 2 separate entries:
www.example.com/events?eventId=123&a=b
www.example.com/events?eventId=123&a=c
In order to get around this, you will need to as tszming mentioned in his answer: track with a custom event (or custom variable), or else override the default page name with something of your choice. Or you can do both; each method is basically a different way of looking at the same basic data, and allows you to do slightly different things. But for answering your basic question of "how many people went to {eventId} version of the page, all of the options will answer that.
sidenote: in any case, all of these solutions involve getting a query string param. GA does not have a built in method for grabbing a query string parameter value, so you will have to grab that value yourself. Unfortunately, javascript does not have a built in way of doing it either. Fortunately, it's pretty easy to write a function that will do it for you (just google "javascript get query param").

Related

Cross Domain Conversion Tracking Google Analytics

I am trying to set up my analytic account and wondering if anyone can help shed some light on what I may be doing wrong. I have 6 e-com sites with products and the shopping cart is on a 7th separate site. Each site has its own unique domain.
I have a property set for each domain and then one additional property that is set as a global account. The theory was to track for each separate property and then additionally in the global account. This might be overkill since I am not overly concerned with traffic on the individual sites. But what I am having trouble with is I am paying for adwords so the ad lands on a shopping1.com and checks out on shopping7.com its not registering as a conersion since there is a switch in the domain name.
Here is the code that I, any input on how to make all 7 sites work as one and adwords realizing there was a conversion would be greatly appreciated. Do I need to set the domanname element? Also, do I need to add code to everything that links back and forth to each of the separate sites?
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-site1']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
_gaq.push(['global._setAccount', 'UA-global']);
_gaq.push(['global._setAllowLinker', true]);
_gaq.push(['global._trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
The code is okay, but not sufficient. First yes, you should use _setDomainName (with the value set to the respective domain names shopping1.com, shopping2.com etc).
More importantly you need to attach the linker functions to links, forms and iframes (and redirects, although with redirects you need to do the linking yourself) - basically every place where people change from one of your domains to another (it's not necessary to attach linker function to links that do not go to your domains).
Google Analytics uses cookies to store data, which cannot be shared between domains. The purpose of the linker function is to attach session information to the url so it is transmitted to the other domain, where it is read by the Google script and stored into a new cookie. Take a look at the documentation to see how the _link and linkByPost-functions are used.
If you are doing redirects you need to be careful that the redirect keeps the Google parameters.
Linker functions result in very long and ugly urls. Cross domain tracking works much better with Universal Analytics where you only have to transmit a single parameter (the client id) between domains.

How to use one Google Analytics code on a network of sites?

And how to set it up?
When I go to Adming -> +New Account, I need to enter a) Website name, b) Website URL, and c) Account name. I would like to use the code on a network of 25 sites, and there is no option not to enter a URL. When I enter it, it displays in the Analytics code that I get afterwards.
When I looked at a Google Analytics code on one of my sites, which is over one year old, it looks something like this:
<script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-01234567- 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>
When I am getting a code right now (which is suppose to be the code for a network of sites), 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-41928014-1', 'test.com');
ga('send', 'pageview');
</script>
it contains a URL / domain name in it.
In general I know that I need to set up an account, and apply filters (to have access to data for individual domains), but I am not sure how to do it step by step (especially with the test.com in the analytics code, as in the example above). Maybe there is a different / better way to do it?
Thank you.
'test.com' is optional an optional parameter in that it only provides a reference for subdomain tracking, so you can leave that off and put the code on all your sites; however, if you have subdomain for those 25 sites, you'll run into problems not being able to track (see documentation https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#cookieName)
I take it that you are trying to have a Roll-up profile for all of your sites? That's great and all, but I would highly recommend creating a separate property for each, as if your sites' get a lot of traffic, you could run into sampling.
Have you heard of Google Tag Manager - http://www.google.com/tagmanager/ ? I would look into this if you have that many properties, as it will make updating any changes to those tags far easier and take much less of your time.
Ideally, you set it up like this:
1) create a container (apply this container code to all your websites)
2) create 25 Google Analytics Tags in GTM and setup a rule that if {{url}} equals test1.com, then use test1 Google Analytics tag.

How to debug Google Analytics Tracking error (_gaq.push returns '3')

SUMMARIZE:
I misunderstood the usage of content script. And that leads to this issue. Here is a quote from Google's official doc:
Execution environment
Content scripts execute in a special environment called an isolated
world. They have access to the DOM of the page they are injected into,
but not to any JavaScript variables or functions created by the page.
It looks to each content script as if there is no other JavaScript
executing on the page it is running on. The same is true in reverse:
JavaScript running on the page cannot call any functions or access any
variables defined by content scripts.
More details:
Content Script tracking with Google Analytics
--
I am adding some tracking code into a Chrome Extension. But when I test it, the _gaq.push returns number 3. As I tested, I figured that if I call _gaq.push in the extension, the number will keep rising. But when I call it in the console, it's all fine (all the same code).
I am wondering how I can do to track deeper into this error?
Thanks very much for every answer!
UPDATE:
When I trace the return value of _gaq, it is still an array. That means ga.js is not loaded, right?
But it seems ga.js is never loaded in my script. Unless I manually type _gaq in the console, it's an object. This is very weird.
I checked the DOM and found that tag is already added.
I reviewed the Network panel, find that ga.js is not loaded at all in the queue. But why I can still use console to access the _gaq object?
UPDATE2:
I use console.log to track the value of "window._gaq" and found that the return value of console.log(window._gaq) is DIFFERENT than directly type "window._gaq" in the console. It's totally two different objects. I even use a setInterval function to log the results and it's keeping returning the array, not the expected object.
When ran through your script _gaq is jut a regular Array. And just like any Array in JavaScript it has the method push that inserts one or more elements into the Array and returns the final length of the array.
eg:
var _gaq = [];
> undefined
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
> 1
_gaq.push(['_setDomainName', 'mysite.com']);
> 2
_gaq.push(['_trackPageview'],
['_trackEvent', 'cat', 'act']);
> 4
After the ga.js file is loaded the _gaq Array is read so the instructions pushed into it can be processed and then the _gaq Array is replaced with an object. The object implements the push function as well. But notice that in this case the push function will execute an instruction into the ga.js library and then return 0.
eg:
var _gaq = {
push: function(){
// Do something here
return 0;
}
};
> undefined
_gaq.push(['_trackPageview'])
> 0
That's a very smart Google design to create an API that can be used even before the library (ga.js) has been loaded.
That's why during your script _gaq.push is a method of the Array class and return increasing numbers and on the console it always returns 0.
This is not a problem at all. It seems to be working as intended.
UPDATE
It seems that ga.js is already loaded. Maybe it was cached and not showing on the network panel.
ga.js is loaded by this part of the tracking script:
(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);
})();
It is loaded asynchronously. So after this snippet is executed it will be loaded but the exact point where it is loaded is unknown.
If you need to know when the ga.js file is loaded, you can push a callback into _gaq and it will be executed when the ga.js file is loaded.
eg:
_gaq.push(function(){
console.log('ga.js loaded!');
debugger;
});

Tracking Individual Users with Google Analytics Custom Variables

I've been working on a support center for my company and we need to track individual users when they login. If possible we would like to track details as well such as pages visited and time spent on the site as well.
I'm able to track how many people login to the site using a custom variable, but I am unable to track individual users. Here is the code I've been using to try to grab the individual user id:
$(document).ready( function() {
var welcomeEmail = document.getElementById('welcome_email').innerHTML;
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-30086012-1']);
var welcomeEmail;
if( $('#welcome_email').length > 0 ) {
//This block of logic makes sure that the welcome_email element actually exists, it will not exist if a user is not logged in yet
welcomeEmail = document.getElementById('welcome_email').innerHTML;
}
_gaq.push(['_setCustomVar',1,'UserEmail',welcomeEmail,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);
})();
What am I missing/doing incorrectly. Appreciate any feedback.
I would say the privacy policy has changed.
You will not (and will not allow any third party to) use the Service to track, collect or upload any data that personally identifies an individual (such as a name, email address or billing information), or other data which can be reasonably linked to such information by Google.
A UserID will not expose to Google any PII about a visitor, and that seems to be what they're concerned about.
Just for the record. Here is a guide by Google on how to accomplish this:
https://developers.google.com/analytics/devguides/collection/analyticsjs/user-id?hl=en
IMPORTANT
It does require you upgrade your property to the Universal Analytics.
UPDATE 2: As of April 2nd 2014 this feature is out of Beta and widely available.
That is a violation of Google Analytics terms of service. See number 7 PRIVACY.
7.PRIVACY . You will not (and will not allow any third party to) use the Service to track or collect personally identifiable information of Internet users, nor will You (or will You allow any third party to) associate any data gathered from Your website(s) (or such third parties' website(s)) with any personally identifying information from any source as part of Your use (or such third parties' use) of the Service. You will have and abide by an appropriate privacy policy and will comply with all applicable laws relating to the collection of information from visitors to Your websites. You must post a privacy policy and that policy must provide notice of your use of a cookie that collects anonymous traffic data.
And
While the username or user ID is not directly PII, if it is used to tie to a person from a backend system…that’s a violation of the Terms of Service.
Google Analytics is not the tool to use for this type of tracking. A custom backend solution that is hosted on your own servers is the better way to go.
This article refers to the new policy. Setting a userID that is setup using the customvariable option in Google Analytics is NOT a breach of their privacy policy.
Please note:: allows Google to personally identify << is this meant, if google can't indentify this user is ok with their policy?
You will not upload any data that allows Google to personally identify an individual (such as certain names, Social Security Numbers, email addresses, or any similar data), or data that permanently identifies a particular device (such as a unique device identifier if such an identifier cannot be reset), even in hashed form.
If you upload any data that allows Google to personally identify an individual, your Google Analytics account can be terminated, and you may lose your Google Analytics data.
https://developers.google.com/analytics/devguides/collection/protocol/policy

google analytics returning visitors number drop after subdomain-tracking related tracking code modification

last friday we switched our website, www.ourweb.com tracking from "single domain" to "one domain to multiple sub-domains", to track both www.ourweb.com and blog.ourweb.com
so i modified the code on the site:
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-1594022-3");
pageTracker._trackPageview();
</script>
to (note we used traditional snippet before):
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12345-1']);
_gaq.push(['_setDomainName', '.ourweb.com']);
_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);
})();
but over last few days, site reported almost double returning visitors drop! from stable 7 000 down to 4 000
only returning visitors number changed that much, new visitors number keeps being similar (around 10 000)
i read this http://www.roirevolution.com/blog/2011/01/google_analytics_subdomain_tracking.php
and they write there that "The leading period causes cookie resets.". is that the reason of such big visitors drop? should i remove the leading period? and if not, what else could be the reason?
ps. i just noticed that probably it's referrals only problem. amount of visitors from referrals decreased from 4000 to around 700 per day after change.
This is a silly quirk of Google Analytics that should be better documented.
There are 2 equally valid but slightly sifferent methods for using _setDomainName.
No Leading Period:
_gaq.push(['_setDomainName', 'ourweb.com']);
Leading Period:
_gaq.push(['_setDomainName', '.ourweb.com']);
Google Analytics uses something called a "domain hash" (literally, a hash of the domain or of the domain you set in _setDomainName) to prevent conflicts between cookies; if the domain hash of the domain you've configured does not match the Google Analytics cookies that it finds, it creates a brand new visit (and a brand new visitor).
The difference is this: By default, if you don't configure your setDomainName, your root www domain gets a "domain hash" that is the same as if it is just http://ourweb.com. That's presumably to prevent self-referrals for traffic between www. and no www.
However, if you add the leading period, the "domain hash" changes. None of your returning visitors will have a matching domain hash. So, when they enter your site, Google Analytics doesn't see a matching set of cookies with the correct domain hash and creates a new visitor id, and all your traffic since the change is totally cut off from the traffic before the change.
As a result, if you have long-standing tracking without using _setDomainName, and you'd like to add support for tracking across subdomains, the way to do so with the last damage is to use no leading period.
You can "recover" the old cookies by switching to using no leading period. If you do so, you'll lose the cookies since you've made the change, though.
The only benefit in using the leading period is that it provides compatibility for third level subdomains (ie, foo.bar.example.com).
visitors drop happened because there was no straight redirect between ourweb.com and www.ourweb.com
ourweb.com was displaying same site as www.ourweb.com, but with all links pointing to www.ourweb.com, and all 3000 return visits drop came from self referrals.
thus cookies were split between those two sites and created fake unique return visits (each time visitor visited ourweb.com and clicked f.ex. link to about.html, he was going to www.ourweb.com/about.html, so ga counted it as two independent unique return visits)
so basically reporting was wrong before and became more accurate.

Resources