Target Offers & Content at Visitors With Google Analytics
One of the lesser known features of Google Analytics is that you can use it to target offers and content through JavaScript - much like the features of BTBuckets and many split testing software providers. Unlike these providers however, using Google Analytics can be a cleaner, tidier way that has less of an impact on page load times (since it only needs the GA script to be loaded).
How it works
1. You set a custom variable at the visitor level scope (page and session level doesn’t work)
2. Using JavaScript, you read the contents of visitors’ custom variable slots
3. Based on the contents, you can show an offer or message to particular visitors
You can use it for targeting visitors of all sorts of segments - Customer Lifetime Value (high value, low value, none), User type (registered, paying customers, non-member), Facebook demographic data (if you’re using the Facebook Connect API) and many more. I’ll leave that up to you…
The code that does it
It’s all based around the function, _getVisitorCustomVar() - have a read up on Google’s documentation about it here.
Simply place the slot number inside _getVisitorCustomVar() where your visitor level variable is set:
_gaq.push(function () { var segment = _gat._getTracker('UA-1XXXXXX-X'); var bucket = segment._getVisitorCustomVar(1); if(typeof(bucket) == undefined) {bucket = '';} if (bucket == 'xyz') { // Do this... } });
And if you’re using the old synchronous version (or perhaps you just NEED the content to be loaded synchronously):
var segment = _gat._getTracker('UA-1XXXXXX-X'); var bucket = segment._getVisitorCustomVar(1); if(typeof(bucket) == undefined) {bucket = '';} if (bucket == 'xyz') { // Do this... }
To customise it further, please try my targeting generator script.
Notes:
- Unless you’re going to re-use the tracking objects we created in the snippets above, you don’t need to update the UA number.
- Since _gaq.push and _getVisitorCustomVar alone will simply return the position within the array, we needed to create a synchronous tracking object to extract it.
- Only visitor level custom variables work - page and session levels just don’t work (_deleteCustomVar() is your friend).
- For the synchronous version of the script, you MUST load ga.js synchrnously before the targeting script.
Recommended: Targeting Generator
Alternatively, you can use my free Google Analytics targeting generator to handle all of the customization for you.
Use the Google Analytics Targeting Generator
Why not use cookies and server-side scripting?
Sure! I just find this method integrates well with Google Analytics and your segments show up well within your reports. Also, you don’t need redundant scripts for reading/writing cookies.
Did you can provide some working example with this code? Code example and screenshot of GA report? Thank you.
Hi Alexander,
You can find the working code and an example of it right here:
http://www.crotricks.com/content-targeting-ga/example.html
If you’d like to customize the code, there is a simple targeting generator script that I have built:
http://www.optimisationbeacon.com/tools-tweaks/google-analytics-content-targeting-generator/
Try it out and let me know if you have any troubles.
Hi Rob,
just stumbled upon this post… how can I return the value of the custom variable from the function in order to use it with another variable or write it into the document?
Thanks! Frank