The simple implementation for tracking visitor actions, or clicks, involves adding the _trackPageview()
function
to an HTML tag. For example, to track a visitor click on an image, just add
_trackPageview()
to the onclick
event of that element:
<img src="/image.jpg" onclick="_gaq.push(['_trackPageview', '/image.jpg']);" />
When a visitor clicks on the above image, a pageview will be created for /image.jpg. You can also use this method to track non-HTML files:
<a href="/schedule.pdf" onclick="_gaq.push(['_trackPageview', '/vpv/downloads/pdf/schedule.pdf']);" />PDF</a>
When creating pageviews for non-HTML files, try to use a consistent
naming convention. This will make it easier to identify them in the
reporting interface. For example, you may want to create a virtual directory
structure using _trackPageview()
.
In the previous code example, I added /vpv/downloads/pdf/
to the value passed to
_trackPageview()
(vpv stands for “virtual
pageview”). This makes it easy to identify the non-HTML files in the
reports.
Outbound links are tracked in the same manner:
<a href="http://www.cutroni.com" onclick="_gaq.push(['_trackPageview', '/vpv/outbound/'+this.href]);" />www.cutroni.com</a>
This outbound link will appear as
/vpv/outbound/http://www.cutroni.com
in the reports.
Again, be logical in your naming convention. By placing all outbound links
in the /vpv/outbound/ directory, you
can easily filter the data in the Top Content report or the Content
Drilldown report.
Note
Clicks on outbound links are not “real” pageviews. If you need an accurate count of the number of pageviews your website generates, make sure you filter out any clicks on outbound links. An exclude filter, using the request URI and a filter pattern that matches your outbound link structure, will do the job.
An alternate method for tracking clicks is to use event tracking rather than virtual pageviews. See Chapter 9 for more information about event tracking.
There is an easier way to track outbound links and non-HTML files.
Create a simple DOM script to automatically apply the _trackPageview()
method to links the moment a
visitor clicks them.
The problem with DOM scripts is the browser compatibility. If a browser changes the way it interprets the DOM, the script can break. This actually happens more often than you might think. Another challenge with DOM scripts is keeping them up to date. At the time of this publication, there are no DOM scripts that support the new async version of the tracking code.
Warning
In October 2007, Google announced that automatic file download tracking and automatic outbound link tracking would be included in Google Analytics. It has been almost three years and this feature has still not appeared. Google assures us that it is coming, but we still have not seen it.
If you are tracking file downloads or outbound links, it is critical to your business that you do not wait for Google to launch this functionality.
One modification that I do recommend adding is a timer, especially when tracking outbound links or file downloads. In some instances, the browser can redirect the visitor to the file or requested website before the Google Analytics code can generate the virtual pageview or event. By adding a short timer, you can increase the chances that Google Analytics will record your data.
Adding a timer means you must create a small function that intercepts the visitor’s click, creates the pageview, and lets the browser execute the visitor’s actions. You modify an outbound link like this:
<a onclick='trackClick(this);return false;' href="http://www.redsox.com/" > Red Sox </a>
Next, create a JavaScript function as follows and place it in the
HEAD
tag on all pages where you need to
track outbound links:
<script type="text/javascript"> function trackClick(this) { _gaq.push(['_trackPageview', '/vpv/outbound/'+this.href]]); setTimeout('document.location = "' + this.href + '"', 100); } </script>
Google Analytics uses up to five first-party cookies to track and
store information about a visitor. These cookies, set by the _trackPageview()
method,
track attributes of the visitor, such as how many times she has been to
the site and where she came from. The cookies do not store any personally
identifiable information about the visitor. Here is a list of all the
tracking cookies, their format, and other information:
- __utma
Expiration: 24 months from the last session
Format: domain-hash.unique-id.ftime.ltime.stime.session-counter
The __utma cookie is the visitor identifier. The
unique-id
value is a number that identifies this specific visitor.ftime
(first time),ltime
(last time), andstime
(start time) are all used to compute visit length (along with the__utmb
and__utmc
cookies). The final value in the cookie is the session or visit counter. It tracks how many times the visitor has visited the site and is incremented every time a new visit begins.- __utmb
Expiration: 30 minutes from the last action (pageview, event, transaction)
Format: domain-hash.session-pageview-count.session-event-count.stime
The __utmb cookie, in conjunction with the __utmc cookie, computes the session length.
- __utmc
Expiration: End of the browser session
Format: domain-hash
The __utmc cookie, in conjunction with the __utmb cookie, computes visit length.
- __utmz
Expiration: By default, 6 months, but you can customize this value
Format: domain-hash.ctime.nsessions.nresponses.utmcsr=X(|utmccn=X|utmctr=X|utmcmd=X|utmcid=X|utmcct=X|utmgclid=X)
The __utmz cookie is the referrer-tracking cookie. It tracks all referrer information regardless of the referrer medium or source. This means all organic, cost-per-click (also known as CPC), campaign, or plain referral information is stored in the __utmz cookie. Data about the referrer is stored in a number of name-value pairs, one for each attribute of the referral:
- utmcsr
Identifies a search engine, newsletter name, or other source specified in the
utm_source
query parameter.- utmccn
Stores the campaign name or value in the
utm_campaign
query parameter.- utmctr
Identifies the keywords used in an organic search or the value in the
utm_term
query parameter.- utmcmd
A campaign medium or value of the
utm_medium
query parameter.- utmcct
Campaign content or the content of a particular ad (used for ad testing). The value from
utm_content
query parameter.- utmgclid
A unique identifier used when AdWords autotagging is enabled. This value is reconciled during data processing with information from AdWords.
- __utmv
Expiration: 6 months
Format: domain-hash.value
This is a custom variable cookie. This cookie is not present unless you have implemented a visitor-level custom variable. The cookie is created using the
_setCustomVar()
method, which we will discuss in Chapter 10.
Note
Google can change the number and format of the cookies at any time. Before using any of this information, you should check the format of the cookies to ensure they have not changed.
Google Analytics uses first-party cookies. A first-party cookie belongs to your website. Some web analytics tools use a third-party cookie. A third-party cookie is not owned by your website, but rather a different website. By default, many browsers block third-party cookies, which can cause issues for any tool that tracks visitors with a third-party cookie. It is always preferable to use an analytics tool that tracks visitors with first-party cookies.
There are numerous studies, white papers, and blog posts estimating the rate at which user cookies are blocked by browsers and deleted by users. Eric Petersen first wrote about the pitfalls of cookies in a 2005 study for Jupiter Research. The latest formal cookie study was done by comScore, an online measurement company: http://troni.me/ca7sDU.
In my opinion, the best course of action to mitigate cookie deletion and its effects on your data is to look for trends and patterns in your data and to avoid absolute numbers.
Get Google Analytics now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.