Customize Your Google Analytics Tracking Code
The new ga.js tracking code has some tricks up its sleeve.
The new code has some built-in functionality that is more advanced than typical implementations require. Broken out by category, some of the advanced options are described below.
These are advanced features and should be used with caution. Using these features for the wrong situations may cloud your reports with funny data, and it won't be possible to fix that bad data once it's in the account.
Keep in mind that these are methods, not functions. Append them to the object name with a period, like this: pageTracker._trackPageview()
Unless stated otherwise, these methods should be called within the block of tracking code that goes on each page before _trackPageview is executed.
Several methods change the way that Google Analytics interprets and reports traffic to your website.
Google Analytics determines whether a visit came from a search engine by comparing the referring website to a predefined list of search engines. This list contains both the domain name of the search engine and the query parameter the engine uses to hold the search term.
There are two methods available for customizing this list.
- _addOrganic() adds a search engine to the list. It accepts two parameters, the domain and the query parameter. For example, pageTracker._addOrganic("google", "q") would add google as a search engine (no subdomain or TLD are required) and identify q as the query parameter containing the search term.
- _clearOrganic() clears the predefined list of search engines. It accepts no parameters. It could be used to build a custom list from scratch.
Searches as Direct Traffic
Let's say that visitors commonly type your URL or company name into search engines. They meant to make a direct visit. You can specify that certain search terms should be counted as direct traffic with _addIgnoredOrganic().
This method accepts one search term as a parameter. To create a list of search terms to ignore, call this method once for each term.
Referral Sites as Direct Traffic
If a partner site has links to your website, you may not want those visits showing up as referrals. To make them appear as direct traffic instead, you can use _addIgnoredRef() and pass the domain name as a parameter. Calling this method multiple times with different domain names will construct a list of domains to count as direct traffic.
The entire list can be cleared by calling _clearIgnoredRef().
Several methods modify the way Google Analytics tracks visits and provide tools for more complex dynamic programming.
Google Analytics sets several cookies that can be customized. By default, if a visitor is idle on your website for 30 minutes, their visit will expire and the next click will count as the beginning of a new visit. For content and news sites, this can sometimes pose a problem. You can change the idle timeout time with _setSessionCookieTimeout() and passing it the new timeout time in milliseconds. Default is 1800000 milliseconds.
Google Analytics also tracks a visitor's traffic source in cookies for six months from the time of their initial visit. That cookie is only refreshed if the visitor comes back to the site through a different traffic source. This expiration date can be changed using _setCampaignCookieTimeout() and passing it a new value in milliseconds. Default is 15768000000 milliseconds.
Finally, Google Analytics uses a cookie to determine whether the visitor is new or returning. This cookie expires after two years, and is refreshed with each pageview. To change this expiration date use
_setVisitorCookieTimeout(). The default value is 63072000000 milliseconds.
Google Analytics tries to track each individual visit to your website. If your site has a large volume of traffic, this can make long-term reports in the interface take much longer to load. You can instruct the tracking code to only capture a sample of the traffic by calling _setSampleRate() and passing it an integer representing a percentage. (i.e., "50" would mean 50%) You determine what sample size is statistically significant enough.
There are methods that allow you to specify whether certain things should be tracked and to determine whether they are currently being tracked.
_setClientInfo() is on by default and tells the code to detect a visitor's browser, connection, screen resolution, etc. Passing this method 0 turns it off. 1 turns it on.
_getClientInfo() will return either a 0 or 1 if the same information is being detected.
Simlarly, _setDetectFlash() will make the code look for Flash on a visitor's browser.
_getDetectFlash() returns a 0 or 1 if the code is looking for Flash.
The _trackPageview() method is the foundation of the Google Analytics tracking code. In the standard code, it sends information to Google's servers that the current page was viewed, and it reports the URL in the address bar. Every time it is executed, it reports another pageview.
Passing _trackPageview() a parameter overwrites the URL in the address bar with a "virtual URL". This might be used for a contact form that uses the same URL for the form and the confirmation page. Dynamically calling "/contact-thanks.php" only when the form is submitted, for example, would allow you to track whether a visitor submitted the form.
Downloaded files can be tracked simply by creating an onClick event for the download link and calling _trackPageview() in that event with the file name passed as a parameter.
Finally, there are a few methods that allow you to detect and set some custom variables for the account.
Google Analytics can be configured to run concurrently with Angelfish Software by sending __utm.gif hits to your own log files, too.
_setRemoteServerMode() is the default method. It requests a __utm.gif only from Google's servers. This will populate reports in Google Analytics.
_setLocalServerMode() requests a __utm.gif only from your own servers. This will not populate Google Analytics reports. It will only populate Angelfish reports, if it's installed correctly. (This requires putting the __utm.gif file at the root of your own website.)
_setLocalRemoteServerMode() requests the __utm.gif from Google's servers as well as your own server. This will populate both Google Analytics and Angelfish reports. You must have a __utm.gif file at the root of your website for this to work.
_getServiceMode() is used to determine whether the code is looking for remote or local gif files. It returns a 0 for local, 1 for remote and 2 for both. 1 is the default.
None of these methods accept any parameters.
You can send traffic data to multiple profiles by calling the second block of tracking code twice. You just need to define a new object in the first line and change the UA number in the _getTracker() method. This will create two tracking objects on the page, each one independently sending information to different profiles.
Having two independent objects means that all the same methods will need to be executed for each one. Only if _trackPageview() is executed for each object will a pageview be reported in each profile, for example.
Account and Version
Finally, there are a few methods that allow you to gather high-level information about the code.
_getAccount() returns the UA number the object is sending data to.
_getVersion() returns the version number of the tracking code itself.
Wow. That's enough information to be very dangerous. Proceed with caution.