Welcome to Part 1 of How to Track User Clicks using Google Analytics. Before we learn how to track clicks we need to review some of the technology behind Google Analytics. In particular, we need to understand how GA creates pageviews. Why? Because we will use the same technology to track clicks. This post may seem off topic, but it lays the foundation for the next post in the series.
urchinTracker: A JavaScript Function
It all starts with the Google Analytics tracking code:
I’d like to point out the second to last line. It contains a JavaScript function called urchinTracker
. For those of you that don’t know what a function is, it’s a piece of code that actually ‘does stuff’.
There are two important features of a JavaScript function:
- It can appear almost anywhere in your web page. There are certain limitations, but in general you can move it around.
- It can appear more than once.
Applying these characteristics to urchinTracker
means we can place urchinTracker
almost anywhere in a web page AND we can include it multiple times, two important concepts.
urchinTracker: What it Does
We know that urchinTracker
is a function and it ‘does stuff’, but what exactly does it do? While visitors engage with your website, urchinTracker
is collecting information in the background. It identifies things like:
- Where the visitor came from
- How many times they’ve been to your site
- What content they are viewing
- Characteristics of their browsing environment (operating system, browser, etc.)
- The title of the current website page
- etc.
Please note that urchinTracker
is not collecting any personally identifiable information. It is collecting generic information about the visitor.
After it collects the data, it sends it to Google Analytics in the form of a pageview. So every time urchinTracker
runs, or as programmers say ‘executes’, it creates a pageview in Google Analytics. And, because urchinTracker
is a function, we can add it to a web page multiple times. This means we can create multiple pageviews in Google Analytics every time a single page loads in the visitor’s browser.
‘Naming’ Pageviews with urchinTracker
Another important concept to understand is how urchinTracker
‘names’ pageviews. When I say ‘names’ a pageview, I mean the way that the pageview is represented in the GA reports. Let’s look at some pageviews in the Top Content report:
Everything in the ‘Content’ column was created by urchinTracker
. Here’s how:
urchinTracker
extracts the information from the location bar of your browser.- It modifies the value to show only the name of the directories, file and query string variables.
Here’s an example. This URL:
would appear in the top content report as this:
That’s the default behavior of urchinTracker
. Recapping what we’ve learned; whenever a page loads in your browser, urchinTracker
collects all the necessary information, creates a pageview name from the location bar and sends the data off to Google Analytics in the form of a pageview.
Important Stuff Below, Pay Attention :)
What’s really cool is that we can over-ride the default behavior and tell urchinTracker
how to name a pageview. For example, let’s say we want to change the way the pageview for ‘/index.php’ is named in GA. We would modify urchinTracker
as follows:
urchinTracker('ski-bum');
This modification forces urchinTracker
to name the pageview ‘ski-bum’ rather than ‘/index.php’. This means that when you view the Top Content report you’ll see ‘ski-bum’ and not ‘/index.php’. Whatever you place in the parenthesis of urchinTracker
becomes the name of the pageview in Google Analytics.
Summary
There are a few important concepts to take away from this post:
- Google Analytics pageviews are created by a JavaScript function named
urchinTracker
urchinTracker
can appear almost anywhere in a web pageurchinTracker
can appear multiple times in a web page- By slightly modifying
urchinTracker
we can change the ‘name’ of pageviews in Google Analytics
In Part two of this series we’ll put our knowledge of urchinTracker
to work and talk about tracking visitor clicks.
Dear Ski Bum:
So let’s say that someone put their GA code into an include file that is in the footer of all their pages. (No lectures on the merits of having an include file that only contains the GA code required. Someone else already claimed that space.) Then, you decide, oops, we really needed to track that onclick event high up on the page. In order to track onclick stuff, the GA code has to load first. So we say, no problem, we’ll just put an extra copy of the GA code high up on the page for this one page. But then there are two copies of urchintracker that get executed. If two copies of urchintracker execute, doesn’t the GA see two page views incorrectly?
And if so, what are the good reasons (if any) for having the code more than one time on a page?
If you have two urchinTracker() calls on a page then you will get two page views even though the visitor is viewing just one physical page. That’s a problem.
The only reason I can think of having two urchinTracker()s, in their default configuration, on the same page is if you were tracking the data in two different accounts.
If you need to fix a situation like the one you describe, just include the javascript tag that pulls the urchin.js from the google-analytics.com AND the account number. You don’t need to include the entire JavaScript block. Just include this part:
<script xsrc=”http://www.google-analytics.com/urchin/js” mce_src=”http://www.google-analytics.com/urchin/js” type=”text/javascript”></script>
<script type=”text/javascript”>
_uacct=XXXXXXX-X
</script>
A similar question… we recently asked a client’s IT dept to implement the GA code on their website so that we could track activity. Unknown to us, they were already using GA.
This has resulted in two instances of the GA code at the bottom of the site’s pages, as in the sample code below (I’ve X’ed out the account numbers.)
_______________________________
_uacct = “UA-XXXXXX-X”; urchinTracker();
_uacct = “UA-XXXXXXX-X”;
urchinTracker();
_______________________________
From your post, it sounds as though there is no issue with having two instances of this code on the same page. However, we are getting significantly lower traffic than the client is reporting… they are reporting thousands of sessions per month and we are reporting… 8. We’ve examined every page of the site and our tracking code is in place correctly on all pages. However, our GA code block consistently follows theirs.
Any ideas? Any insight is much appreciated!
-Peter
p.s. code block didn’t reproduce properly… not sure why. It is exactly the default GA code block twice prior to the closing /body tag.
Thanks!
Hey Peter,
To track data in multiple GA accounts you need to ‘reset’ the GA code between calls to urchinTracker. There is a variable, named _uff that can be used to do this. Your code will look something like this:
_uacct = “UA-XXXXX-1”;
urchinTracker();
_uff = 0; // Reset for second account
_uacct = “UA-XXXXX-2”;
urchinTracker();
Check out this thread in the GA Help Group:
http://groups.google.com/group/analytics-help-basics/browse_thread/thread/3e2ccb1622d0c6d0/bc23b655ad023a92?lnk=gst&q=_uff&amp;amp;amp;amp;amp;rnum=1#bc23b655ad023a92&utm_id=bl
Justin
Justin,
Thank you very much – this is a real lifesaver!
-Peter
Justin,
Thanks again for sharing this insight – just one last question…
I see that the accounts are listed as UA-XXXXX-1 and UA-XXXXX-2. Is the “1” and “2” meaningful and required?
Presently, the account numbers provided for the two GA accounts I am using both end in “-1”. Should I change the “-1” to “-2” on the second account listed?
Thanks!
Hey Peter,
The last number does not matter. I should have used a different example in my commetn.
Justin
Hey Justin,
Great Blog! Thanks for your wisdom.
Just a question on the multiple account trackin. I have implemented this setup successfully but I am having trouble with the E-commerce aspect of it. It seems that the data is only sent to 1 of the 2 accounts.
Any ideas how to fix this?
I have to mention that I use a shopping cart in another domain.
Thanks
Hi John,
I think you need to call the utmSetTrans() twice. That’s the function that actually sends the data to Google Analytics. If you only call the function once, then the data will only go to one account. Try something like this:
_uacct = “UA-XXXXX-X″;
__utmSetTrans();
_uff = 0; // Reset for second account
_uacct = “UA-YYYYYY-Y″;
__utmSetTrans();
I’ve never tried this, but it makes sense that it would work. Let me know how things turn out.
Justin
can you use urchintracker for websites such as myspace? and what kind of information would it give about the person who visits your web page?
thanks,
Sarah
Hi Sarah,
To the best of my knowledge you can not add the GATC to a MySpace page. Therefore you won’t have access to the urchinTracker() function.
If you use MySpace for marketing purposes, and send traffic to some other site, you could use link tagging to understand how much traffic you get from MySpace.
Hope that helps,
Justin
I need to create a custom search and replace script
for
http://www.workspaceadvisor.com/i7:Web,workspaceadvisor,getstarted,home~~00000000.00000000.00000000.01010002.5105002A.4445820A.45000A0A.460255FE
I want to strip all text after the ~~
Help.
DK
Hi David,
You want to use an advanced filter to modify the request URI. Here are the settings:
Filed A: Request URI Extract A: (.*?)~~
Filed B: – Exrtact B: –
Output To: Request URI Constructor: $A1
All the other settings can be left with their default value.
One note, I don’t know what other filters you have applied. This filter could be affected by other filters applied to the profile.
Thanks for reading and good luck,
Justin
Hello and thanks for this great article.
I wanted to customize further google analytics to include some custom attributes I would like to track. GA is good for generic data. But if we want to track other data, as custom values, to be able to report on.
For example, I would like to track ebay listings and website listings. Ebay items are pointing to existing real products existing to the website, so I would like in the ebay page to track the product code. Later on, this will be useful to provide reports on navy or army items view, segmented by view source (ebay or website) and tracking the sales effectiveness of such items. I hope this makes sense..
John
Hey,
Great article. My question is what happens if you do this:
_uacct = “UA-XXXXXXX-X”;
urchinTracker(“help pages”);
urchinTracker(“ABC help page”);
Can we do something like that? Basically, I want to call urchinTracker() twice for a different location each time, so that I can look at some aggregate reports of say “all help pages”, or just the “ABC help page”.
thanks,
-hari
Hi Hari,
Yes, you can use that code and create two pageviews for each page. As long as you’re aware that you’re creating two pageviews for each page.
What I would do is use a filter to create a second profile based on some piece of information the groups the ABC Help pages.
Thanks for reading and I hope that helps.
Justin
hi, how do i GA on my profile in myspace? can somebody pls help me? thezhl
Hi,
The blog is so helpful.
I have the following scripts included in my footer.php file:
_uacct = “XX-XXXXXXX_XX”
urchinTracker(“/register”);
_uacct = “XX-XXXXXXX_XX”
urchinTracker(“/success”);
Now, I need to call them in my register as well as success page. Its like for both of these pages one .php page is executed. Please help.
Thanks,
Jenny
Hi Justin,
Thanks for your blogs, great information!!
I really hope someone can help me with the following because this is driving me insane…
I enabled Google Analytics on my site (old urchin tracking code (I have to use that one.)
In my Google Analytics report I can see all transactions that take place on my site.
(Ecommerce => Transactions)
But: How can I get the information about which referring site a transaction came from (if it was a referral)?
OR:
In a different report I can also see how many transactions came in from a certain referring site and what the associated revenue is (e.g. 3 transactions, generating $x revenue).
(Traffic Sources => [Source] => Ecommerce)
But: How can I get the information about which out of all my transactions are the transactions listed here that came from this referring site?
It looks like all the data I need is there, it’s just not displayed in a way that gives me the information I need (a 1:1 connection between a transaction and the referring site it came from).
Thanks…!
Relly nice guide. Thanks a lot for sharing.
Hi Jenny,
You need to add some server side logic to display the correct tracking code at the correct time. This is usually done using some type of ‘if-else’ logic. The exact code will depend on your current code and programming language.
Hope that helps and thanks for reading.
Justin
Hi,
I have an eBay store with many pages of items for sale. I tried to install Analytics on the pages but it rejects it because java script is part of the insert. How can I install Analytic on my eBay pages?
Hi George,
If eBay will not let you add the tracking code to your auction pages then you can’t use GA to track eBay. You could try adding an iFrame to your description, and place the GA code in the source for the iFrame. I haven’t tried that but it may work.
Best of luck,
Justin
Nick,
The easiest way to create this data is to use an advanced filter that concatenates the source information with the transaction ID. Attach the advanced filter to a profile and all the data in the profile will have the source data in the transaction report.
Hope that helps and thanks for reading the blog.
Justin
Urchintracer is JScript script, so it requires a “clien side” to be opened to generate the action for this scrip. Do you know a way how to use it on the server side? I have a PHP script which redirects requests for files downlopading and I need to track it by GA. My script check the request and do “Localredirect” to point to the real file, but how I can use urchintracker in this case?
Andrey
Hi Andrey,
All GA tracking happens client side and there is no way around that. So if you’re using some type of PHP script to track things you need to implement some type of JS equivalent to send the data to GA.
Thanks for the question,
Justin