In part 1 of this post I explained how to track page views and events in Google Analytics. In this post I will explain
- How to use Google Analytics for logging data and javascript errors.
- Custom Variables in Google Analytics.
- How to use Google Analytics for track traffic sources.
Logging data and Javascript Errors
Google Analytics events could be used for logging custom data and tracking Javascript errors in Google Analytics. For logging data just execute following
1
|
|
This mechanism could also be used to track error codes or messages received from AJAX calls or a REST service call. Following example assumes response.code
contains the error code received in an AJAX call.
1
|
|
We could also use the same approach for tracking global javascript errors that are not handled.
1 2 3 |
|
This snippet simply attaches a callback on the onerror
callback of the window
object for logging errors.
Custom Variables
Google Analytics is pretty good in tracking page views, but a lot of times we need a way to attach custom data to the page views. Custom variables in Google Analytics lets you do this. For setting a custom variable you could add following snippet
1
|
|
In the above snippet, _setCustomVar
has 4 arguments.
index
argument is a slot or the position of the variable. Google Analytics only allows you track 5 variables per page page view and as a result this argument has a value between 1 to 5.name
andvalue
define the data that you are tracking. Think of it as key - value data associated with the current page view.scope
argument defines the duration for which Google Analytics should track this variable. It can take a value between 1 to 3 i.e. 1 (page level), 2 (session level) and 3 (visitor level)- Page level scope means variable would be tracked only for the duration of current page view.
- Session level means variable applies to a single session or visit.
- Visitor level means variable would be tracked across sessions till users removes the Analytics cookie.
Custom Variables gotchas
- You can only set 5 variables per page view.
- Do not change the variable names and slots (index) across page views. This will help in keeping things consistent.
- Custom variable are tied to the current page view. So, any variable that you want to be tracked with the page view should be set before the
_trackPageview
call.
Custom Variables uses
You might wonder what can I do with custom variables, well possibilities are endless. Once you start tracking some custom information then you could go in Google Analytics and do some custom analysis to understand how users behave with your website. Following are some examples of what to track using custom variables
- Identify type of user who viewed the page e.g. visitor, member, gold-user, platinum-user, etc
- Track demographic information e.g. age, gender, etc of users who viewed the page.
- Track how user arrived at the page e.g. paid search campaign, RSS, email newsletter, social n/w, etc
- Track users who successfully shopped on your website.
- Track users who contacted customer support.
Track Traffic Sources
When the visitors start coming to your website then one of the metrics that you would want to track is who is referring users to your website. Users might have discovered your website through a search engine like Google, Yahoo, Bing, etc or might have come to your website by following a link from the newsletter you sent. But, if you want to track other sources then you need to format your inbound URL with following attributes
utm_source
: name of the traffic source e.g. bing.com, email newsletter, social, etcutm_medium
: medium through which user came to your website e.g. cpc campaign, email, RSS, etcutm_campaign
: string that lets you identify you campaigns e.g. bing_campaign_1, Spring 2013 newsletter, etcutm_term
: keywords or tags that you want to associate with the source in Google Analytics
Using these attributes an inbound URL in email newsletter would be formatted like
1
|
|
Google Analytics automatically tracks the traffic sources, keywords, and type of search i.e. organic or paid (Ad Words campaign) for all traffic coming from google.com What this means is that if you are using an Ad Words campaign then you don’t need to do anything. But for traffic coming from Bing Ads it will not track the keywords and campaign details. Luckily this could be easily fixed but you would need to format the destination URL of the ads in Bing Ads account to use the utm_source
, utm_medium
, utm_campaign
, and utm_term
query parameters. If the bing campaign name is campaign_1 then this URL would be formatted as shown below
1
|
|
In this URL pay attention to {QueryString} this should be entered exactly as shown with curly braces as Bind Ads platform will dynamically replace with search terms when the ad is served.
Follow me on twitter here