= Google Analytics Power User Tips = [[http://www.google.com/analytics/|Google Analytics]] is the best free traffic analysis software available. It can handle pretty much any sized website (at least within reason), and has a number of fairly advanced capabilities. == Basic Install == Basic Install is pretty well covered by [[http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=66983|Google's Docs]]. **Be sure to use the new ''ga.js'' code**, not the old ''urchin.js'' code. Make sure that the tracking code appears once, and only once, on all of your pages. Preferably at the very bottom, right above the closing '''' tag. Once you get that done, login and start looking at your reports. Note that (annoyingly) any configuration changes you make to your reports (e.g. apply a new filter, change your default homepage filename, etc.) only affect data from that day onward. You can't go back and reprocess old data. === Tracking Terminology === Google Analytics has five tracking areas: //Dashboard//, //Visitors//, //Traffic Sources//, //Content// and //Goals//. In most reports, Google Analytics tracks pretty much everything via **Visits**, which is one person being active on the site. They can be active as long as they want, but as soon as they are [[http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=57164|inactive for 30min or more]], their visit ends. If they come back, they are counted as a new visit. They also offer **Visitors**; these are unique across the entire time period you're looking at. So if somebody visited on Monday, and then visited again on Wednesday, they'd have two visits but be one visitor. **Pageviews** counts the number of individual pages that has been viewed; a reload of a page means that page is counted again. **Unique Pageviews** filters out reloads (so if you reload a page ten times, that's ten pageviews but one unique pageview). Unique Pageviews only count across visits, meaning if someone views a page, waits 30 minutes (and thus their 'visit' ends), and then views the same page again, that is two unique pageviews (one from the first visit and one from the second visit). === Tracking Goals ==== Each GA profile gives you up to four **Goals** that you can track. You don't have to insert any special code into your pages for these; GA matches your goals based on the URL of the page. In my opinion, the best thing to do is to write regular expression matches against the URL; this allows you a lot of flexibility you wouldn't have otherwise. For example, [[http://www.whmcs.com/|WHMCS 3.4.1]] has a checkout process that can end on //either// ''creditcard.php?invoiceid=XXXX'' OR ''order.php?step=6''. So, you'd make your Goal URL ''(order.php\?step\=6)|(creditcard\.php\?invoiceid\=[\d]*)'', which would match either page. If you make your Goal URL a regular expression, you need to make your goal steps the same, e.g. ''order\.php$'', which matches any URL that **ends** in order.php. === Filters === Filters are one of the most powerful features of GA. They let you include or exclude certain pages/subdirectories, and do search and replace operations on URLs (or other variables). The most important (and most annoying) thing about filters is that they run when the original data is collected, and **ONLY** when the original data is collected. If you make a mistake, or you just want to go retroactively apply a new filter to your data, you can't. == Power User Tips == ==== Using Filters instead of Default Page === GA lets you set a 'default page' in the profile settings (e.g. ''index.html'' or ''index.php''). It then converts any root level (''/'') requests to the equivalent default page URL (e.g. ''/index.php''), and displays those in its reports. The problem is that sometimes you might have those files redirected, or they otherwise can't be accessed at ''/index.php''. You also can't set more than one page to be your 'default page'. A better solution is to setup a filter, which renames ''index.php'', ''home.php'', and whatever else you want to ''/''. Then all of your URLS are listed in GA as ''/'', which is more useful. To do this, setup a 'Search & Replace' filter on the 'Request URI'. Search for ''/+index\.php'' and replace with ''/'' . === Tracking Email via Google Analytics === You can use the GA campaign feature to [[Campaigns and Email Tracking|track marketing campaigns]], including email. === Google Records Cached Hits === If a user loads your page out of the Google Search cache, your Google Analytics code **WILL** run, and you'll record a hit for that page. Generally, this is probably OK... I think a cached hit is still a 'hit' on your site. If you don't want this to happen, you can setup a filter to filter out the cached traffic. === Using _setVar() to track Logged In Users === GA has a 'user defined' segment, and you can pass your own variable into it and then segment your reports on that variable. To do this, call '' _setVar() '' in your code: // any user with $isLoggedIn set to true will have the user defined GA variable // set to 'logged-in' pageTracker._setVar(); pageTracker._trackPageview(); There are a few things to keep in mind: * There is **only one** user defined variable. If you call '' _setVar() '' again, it will overwrite the existing variable. There is a way to [[http://www.lunametrics.com/blog/2008/04/17/stuff-more-than-one-value-in-gas-user-defined-segment/|set more than one]], but it is a bit cumbersome. * The variable is stored in the cookie, and thus is passed on subsequent GA pageviews. This can be good (you don't have to set it again) or bad (the status of the variable changes and you forget to update it). Just keep it in mind. * It should also be noted this variable is passed across entire //visits//. So if you set a visitor to 'logged-in' status ( ''_setVar('logged-in');'' ) on his visit today, and he comes back tomorrow, **tomorrow's visit** will still be tagged with 'logged-in', even though you didn't explicitly set it again. As before, this can be good or bad, depending on what you want to do. * **NOTE:** If you use ''_setVar()'' on every page load, it will break the bounce rate calculation in GA. This is because ''_setVar()'' records a secret second pageview every time it is called. I wrote up [[marketing-tech:google-analytics:setvar-and-the-zero-bounce-rate-bug|some code to fix this 'zero bounce rate' problem]], so ''_setVar()'' is only called once and then the value is read out of the cookie. === Record Data in More than One Profile === If you want, you can record data for the same hit in more than one profile (using codes ''UA-XXXXXX-1'' and ''UA-YYYYYY-1'' in this example): === Tracking Named Pageviews === If you want, you can inject a pageview into the Google Analytics stats. This is done by calling ''_trackPageview()'' again, but with an argument. Generally, you'll want to do this in an ''onclick'', or similar: click to track the foobar page Needless to say, the main google code should have loaded before you try to call this function, but if you're putting it in an ''onclick'', odds are that it has. === Google Analytics Validation Doesn't Like 301/302 Redirects === GA doesn't seem to like 301/302 redirects when validating tracking codes. It's a fairly common technique to redirect ''startupcto.com'' to ''www.startupcto.com'' (or vice versa). If you tell GA that your tracking code is on ''startupcto.com'', and that redirects to ''www.startupcto.com'', GA will never validate that you have properly installed your tracking code. You must set your Website URL to the non-redirected domain, e.g. ''www.startupcto.com''. === Tracking Subdomains === There are numerous ways to track sub-domains in Google Analytics, but I think [[http://www.epikone.com/blog/2007/11/19/tracking-sub-domains-with-google-analytics/|this one is the best]]. The basic idea is using one profile for all sub-domains, and then creatign sub-profiles (with filters applied) for each sub-domain. === Set the campaign cookie timeout using setCookieTimeout() === You may want to set the campaign cookie timeout to something short (such as seven days) rather than the standard six months. For more, see the article on [[old-campaign-data-appearing-in-new-reports|old campaign data appearing in new reports]]. === Google Analytics API === The full GA API [[http://code.google.com/apis/analytics/docs/gaJSApi.html|is available]].