This article is about Classic Analytics, not Universal Analytics which uses a different syntax. While Classic Analytics is set to be phased out over the next three years, many – if not all – of your clients and many websites still use Classic. It’s also great to understand how these work.
As a rule, placing your Analytics code in the header of every page of your site does the job of tracking your site performance effectively.
You don’t need to go through every single page in order to do this – you can simple drop it into the header.php (after the <body> tag).
Sites with Classic Analytics installed will already have standard tracking. But in order to be a bit more advanced when different pages need different tracking treatment, you’ll need to make some edits to the code.
This is what the basic Classic Analytics code looks like:
Adding Virtual Pageviews
First off, this is a simple change in the code which can help you track pageviews when the URL of a certain page does not change.
For example, on a page which brings up an i-frame after a certain button is clicked, the URL will not change even though the page appearance does. Adding virtual pageviews will allow you to get the metrics for this particular event.
By adding the virtual pageview command, you can also turn events into goals.
Add this second parameter to your _trackPageview call:
So for example, if we wanted to track virtual pageviews of a page called “/kitty-mittens.html”, your code should look like this:
Now your virtual pageviews will appear in your Analytics content report, with your normal page views, in your ‘site content’ tab.
Tracking Custom Variables
Custom variables can give you extra information according to what you want to see. This could be tracking page-level, session-level and visitor-level variables. The custom variable code needs to go before the trackPageview line. It’s a bit more complicated:
<number> will be any number between 1 and 5, as defined by your custom variable slots in Analytics.
<name> describes the custom variable itself (for example, ‘membership type’).
<value> refers to the value of said custom variable, as defined by you.
<type>, also called opt_scope, is the type of custom variable shown by numbers 1 to 3:
1 = Visitor level
2 = Session level
3 = page level
(It’s worth noting that when you define your slots in Analytics, if you put two tracking commands into one slot the latter will overwrite the other, and get full credit for each visit.)
So code for a slot 1 page-level variable, with a name of ‘Gender’ and value of ‘Female’ would be:
Tracked events can give you even more of an insight into your consumer’s behaviour.
In this instance, ‘events’ can mean clicking onto certain sections of a page, or downloading certain items (such as items which cannot include their own tracking code).
The (basic) call for this is:
Where ‘category’ refers to the activity category, and ‘name’ refers to the action paired with that activity. This can be playing a video, or removing an item from a shopping cart for example.
Viewing Event Tracking Data
When you have your event tracking codes properly set up on your site, you can see the results by clicking the ‘events’ tab in the ‘Behaviour’ menu of Analytics. Click overview to see your events by category, action and label.
You can also use Advanced Segments here to really get insight on your consumer behaviour.