8

I have been asked by our marketing department to add Google Analytics custom variable tracking to my company's website. As the website uses server side includes, modifications to the tracking tag roll out globally - maintenance is therefore a headache!

So, if I add the following code (keeping in mind SSI so every page has the same code):

// visitor level tracking, id = 12345
// Record a unique id for each visitor. When they return also track this id
_gaq.push(['_setCustomVar', 1, 'id', '12345', 1]);

// page level tracking
// If the user signs up for our newsletter we set newsletter to true
// Each page they subsequently visit should also mark this as true
_gaq.push(['_setCustomVar', 1, 'newsletter', 'true', 1]);

I don't use GA and the marketing people don't use custom variables, so we don't actually know how or if this will work. Therefore my questions are:-

  • Do I want Page, Session or Visitor level tracking?
  • What happens when the same code is used on every page?
  • Can GA 'overwrite' a setting. For example, if I set newsletter to true on page X and then user navigates to page Y, will the variable also be marked there?
3
  • 10
    "You will not (and will not allow any third party to) use the Service to track or collect personally identifiable information of Internet users" - Google Analytics Terms of Service
    – danlefree
    Nov 10, 2011 at 22:48
  • @danlefree - thanks for that. I sent your link off to the marketing folks to fight over.
    – mrtsherman
    Nov 10, 2011 at 22:57
  • @mrtsherman take a look at GetClicky.com they track and show you IP's and quite a bit more information than GA see if it's an option to the marketing department.
    – Anagio
    Mar 12, 2012 at 4:29

4 Answers 4

3

Tracking individual sessions is against google analytics TOS. I have tried it, and after they catch up to you, they simply lock out the data.

A better choice would be to use one of the GA alternatives. CrazyEgg. Gaug.es. and others.

3

While sending Google information that is personally identifiable is simply not permitted, you can instead send an identifier which is known only to you.

You should create what I’m going to term a “Google Analytics identifier” (GAID) which is mapped to the username/user ID and is only used to send tracking data to Google Analytics. You’ll likely need to store this against the user object/user table in your backend system.

I've written up some real world examples of what is and isn't allowed in this blogpost:

Identifying your users in Google Analytics while complying with section 7 of the terms of service

You can then send something like this to Google Analytics:

var gaid = 'some secret identifier'; _gaq.push(['_setCustomVar', 1, 'gaid', gaid, 1 ]);

This means set a custom variable (1st parameter is '_setCustomVar' )...

  • in slot 1 (2nd parameter)
  • named 'gaid' (3rd parameter)
  • with value 'some secret identifier' (4th parameter)
  • for the entire visit (5th parameter)

Once this is done, you'll need to lookup each user against the gaid in your backend system.

Alternatively, you could build your own integration against the Google Analytics API to combine the data from Google Analytics with the data in your backend system.

If you change the content of a visit based custom variable after it is initially set, but in the name visit, the value will be overwritten.

Update:

On your website

Google released Universal Analytics out of beta at the start of April 2014. Now it's fairly easy to setup this kind of thing with the User ID feature

e.g.

<script> 
  // Standard Google Universal Analytics code
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-XXXXXX-Y', {'userId': gaid});   
  ga('set', 'dimension1', gaid);                   // Set a `Custom User ID` dimension too if the user id hasn't been rolled out to you yet.
  ga('send', 'pageview');

</script>

The "User ID" feature is currently being rolled out, so in the meantime, we're using a Custom Dimension to store our own "Custom User ID".

In Google Analytics

  • Browse to Google Analytics Administration
  • Select an Account and then a Property within that account
  • Expand Custom Definitions below a selected Property
  • Select Custom Dimensions
  • Click on the Custom Dimension Name button
  • In the Add Custom Dimension screen
    • Type Custom User ID in the Name field
    • Select Visit from the Scope dropdown menu
    • Check the Active checkbox
    • Click the Create button

Congratulations, you now have a Custom Dimension named Custom User ID which is available for use within the Google Analytics interface. Next, you'll need to populate this custom dimension with some data.

0

Not sure what language you're using, but you could build a session for a user and then "roll your own" tracking if you HAVE to get into that. Writing to a db each page a user goes to can create overhead when writing to a file or DB though.

To ID the user you'd have to create some kind of UUID for their session (could do a random number + server microtom + IP or something similar)

1
  • Thanks for the suggestion digit. I'm afraid that's way more overhead than we are willing to devote. GA provides loads of tools that I have no hope of duplicating. The site is also a simple SSI based site so I have no access to a db.
    – mrtsherman
    Apr 10, 2012 at 20:01
0

You will not (and will not allow any third party to) use the Service to track, collect or upload any data that personally identifies an individual (such as a name, email address or billing information), or other data which can be reasonably linked to such information by Google ... You must post a Privacy Policy and that Privacy Policy must provide notice of Your use of cookies that are used to collect traffic data, and You must not circumvent any privacy features (e.g., an opt-out) that are part of the Service.

http://www.google.com/analytics/tos.html

So you can:

  1. Check for a present cookie SSI-GA-track, for example.
  2. No cookie, you generate one via JavaScript (due to SSI) by appending date, maybe hash of any unique data of browser / anything (to generate a var that will become a cookie). To adhere to TOS, hashing should be ONE_WAY, e.g. md5, sha1, NOT BASE64.
  3. Have cookie -> set var id.
  4. Track!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.