Deprecated: Assigning the return value of new by reference is deprecated in /home/ophir/prusak.com/wp-settings.php on line 512

Deprecated: Assigning the return value of new by reference is deprecated in /home/ophir/prusak.com/wp-settings.php on line 527

Deprecated: Assigning the return value of new by reference is deprecated in /home/ophir/prusak.com/wp-settings.php on line 534

Deprecated: Assigning the return value of new by reference is deprecated in /home/ophir/prusak.com/wp-settings.php on line 570

Strict Standards: Declaration of Walker_Page::start_lvl() should be compatible with Walker::start_lvl(&$output) in /home/ophir/prusak.com/wp-includes/classes.php on line 1199

Strict Standards: Declaration of Walker_Page::end_lvl() should be compatible with Walker::end_lvl(&$output) in /home/ophir/prusak.com/wp-includes/classes.php on line 1199

Strict Standards: Declaration of Walker_Page::start_el() should be compatible with Walker::start_el(&$output) in /home/ophir/prusak.com/wp-includes/classes.php on line 1199

Strict Standards: Declaration of Walker_Page::end_el() should be compatible with Walker::end_el(&$output) in /home/ophir/prusak.com/wp-includes/classes.php on line 1199

Strict Standards: Declaration of Walker_PageDropdown::start_el() should be compatible with Walker::start_el(&$output) in /home/ophir/prusak.com/wp-includes/classes.php on line 1244

Strict Standards: Declaration of Walker_Category::start_lvl() should be compatible with Walker::start_lvl(&$output) in /home/ophir/prusak.com/wp-includes/classes.php on line 1391

Strict Standards: Declaration of Walker_Category::end_lvl() should be compatible with Walker::end_lvl(&$output) in /home/ophir/prusak.com/wp-includes/classes.php on line 1391

Strict Standards: Declaration of Walker_Category::start_el() should be compatible with Walker::start_el(&$output) in /home/ophir/prusak.com/wp-includes/classes.php on line 1391

Strict Standards: Declaration of Walker_Category::end_el() should be compatible with Walker::end_el(&$output) in /home/ophir/prusak.com/wp-includes/classes.php on line 1391

Strict Standards: Declaration of Walker_CategoryDropdown::start_el() should be compatible with Walker::start_el(&$output) in /home/ophir/prusak.com/wp-includes/classes.php on line 1442

Strict Standards: Redefining already defined constructor for class wpdb in /home/ophir/prusak.com/wp-includes/wp-db.php on line 308

Deprecated: Assigning the return value of new by reference is deprecated in /home/ophir/prusak.com/wp-includes/cache.php on line 103

Strict Standards: Redefining already defined constructor for class WP_Object_Cache in /home/ophir/prusak.com/wp-includes/cache.php on line 431

Deprecated: Assigning the return value of new by reference is deprecated in /home/ophir/prusak.com/wp-includes/query.php on line 61

Deprecated: Assigning the return value of new by reference is deprecated in /home/ophir/prusak.com/wp-includes/theme.php on line 1109

Strict Standards: Declaration of Walker_Comment::start_lvl() should be compatible with Walker::start_lvl(&$output) in /home/ophir/prusak.com/wp-includes/comment-template.php on line 1266

Strict Standards: Declaration of Walker_Comment::end_lvl() should be compatible with Walker::end_lvl(&$output) in /home/ophir/prusak.com/wp-includes/comment-template.php on line 1266

Strict Standards: Declaration of Walker_Comment::start_el() should be compatible with Walker::start_el(&$output) in /home/ophir/prusak.com/wp-includes/comment-template.php on line 1266

Strict Standards: Declaration of Walker_Comment::end_el() should be compatible with Walker::end_el(&$output) in /home/ophir/prusak.com/wp-includes/comment-template.php on line 1266

Strict Standards: Redefining already defined constructor for class WP_Dependencies in /home/ophir/prusak.com/wp-includes/class.wp-dependencies.php on line 31

Strict Standards: Redefining already defined constructor for class WP_Http in /home/ophir/prusak.com/wp-includes/http.php on line 61

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/ophir/prusak.com/wp-settings.php:512) in /home/ophir/prusak.com/wp-content/plugins/wordpress-automatic-upgrade/wordpress-automatic-upgrade.php on line 92

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/ophir/prusak.com/wp-settings.php:512) in /home/ophir/prusak.com/wp-content/plugins/wordpress-automatic-upgrade/wordpress-automatic-upgrade.php on line 92

Strict Standards: Redefining already defined constructor for class ftp_base in /home/ophir/prusak.com/wp-content/plugins/wordpress-automatic-upgrade/lib/ftp_class.php on line 56

Strict Standards: Redefining already defined constructor for class ftp in /home/ophir/prusak.com/wp-content/plugins/wordpress-automatic-upgrade/lib/ftp_class_sockets.php on line 8
Google Analytics Referrer Override

Prusak.com

Online Since 1996

Google Analytics Referrer Override

Posted by ophir on November 4th, 2008

A couple of weeks ago I posted about a known issue using A/B experiments in Google Website Optimizer.
http://www.prusak.com/google-website-optimizer-and-referer-data/

Basically, when conducting an A/B experiment in Website Optimizer, the original referrer data is lost because of the JavaScript redirect to the test versions. This means that the traffic source will be wrong (it will say direct instead of the true traffic source).

This issue isn’t just a problem with website optimizer. Any landing page that uses JavaScript to redirect will loose the original referrer data. For example if your site provides easy to remember URLs like www.mysite.com/freeshipping and then uses JavaScript to redirect users to a different URL.

I wrote in my previous blog post that I wished Google Analytics provided an easy way to over ride the referrer value (and not use the document.referrer value).

It seems I spoke to soon.

Google Analytics DOES have a way to overwrite the referrer data.

It just doesn’t seem to be documented anywhere :)

I planned on writing my own hack to override the referrer value in Google Analytics. I was doing some reverse engineering of the ga.js code and found this interesting tidbit:

a._setReferrerOverride=function(b){a.yb=b}

I’m still running some tests, but based on my initial results, the _setReferrerOverride function provides a simple way to manually set the referrer value.

So now, we can actually capture the original referrer even when the visitor is redirected. Here’s how:
1 - Store the original referrer data in a cookie named realreferrer right before we redirect
2 - On the test pages, look for the realreferrer cookie. If it exists use it to over ride the referrer value in Google Analytics and then delete the cookie (so it won’t mess up page views that weren’t a result of a redirect from the original version).

Here’s the code I’m currently using in my tests.
Add this on the original page before the redirect (the GWO control script):

<script type="text/javascript">
document.cookie = "realreferrer="+encodeURIComponent(document.referrer)+"; path=/";
</script>
 

On the test page, add this code before any GA or GWO calls:

<script type="text/javascript">
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(";");
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==" ") c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return "";
}

var realreferrer = decodeURIComponent(readCookie("realreferrer"));
document.cookie = "realreferrer=; path=/";
</script>
 

Then change your GA code from this:

<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXXXX-Y");
pageTracker._trackPageview();
</script>
 

to this:

<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXXXX-Y");
if (realreferrer.length > 0) {
    pageTracker._setReferrerOverride(realreferrer);
}
pageTracker._trackPageview();
</script>
 

Please let me know if this works for you !

Update:
I just found another (and possibly better) solution. See:
http://www.prusak.com/google-ananlytics-initial-referrer-update/

Update #2 [Oct 2010]:
It’s been over two years since my initial posting and this issue still exists. A few people have asked about an async version so here it is.

All of my custom JS stays the same.

Here is the change for the async code:

Before

_gaq.push([‘_trackPageview’]);
 

After

if (realreferrer.length > 0) {
    _gaq.push([‘_setReferrerOverride’,realreferrer]);
}
_gaq.push([‘_trackPageview’]);
 

Please note my blog messes up the single quotes for code snippets when copying / pasting.

- Ophir

12 Responses to “Google Analytics Referrer Override”

  1. Google Ananlytics Initial Referrer Update Says:

    [...] Google Analytics Referrer Override [...]

  2. Tracking Transactions back to the Initial Referrer with Google Analytics | Web PR Today Says:

    [...] no official documentation available for _setReferrerOverride, but I believe credit for discovering this method goes to Ophir. He also gives an excellent use for the function in his [...]

  3. Brandon Thomson Says:

    I don’t know, this solution seems safer than the second to me. And easier to fix if goog decides to mess with their API in the future.

  4. Direct Traffic in Google Analytics | Soupbo Says:

    [...] Overriding the Referrer Datum – use the _setReferrerOverride function to set the referrer value based on a Cookie. [...]

  5. Katya Says:

    Ehi everybody, but this is the old code, but what are talking about new asynchronous code?
    It is the same thing? Or what??
    Thanks a lot.

  6. VIncent Says:

    What are talking about new asynchronous code?
    iniData have been deprecated !

  7. VIncent Says:

    Thx for the update !

  8. Measuring and Running A/B Testing Using Google Website Optimizer and Google Analytics | Portal Analytics Says:

    [...] second solution is to store the referrer in a cookie before the Control Script and on the landing page, use the _setReferrerOverride method to manually [...]

  9. Yoav Says:

    Hi,

    Great post.

    Do you know if this hack will also work for using Goals with funnels that span more than one sub domain ? (the problem is that once a certain page in the goal is in a different sub domain, Analytics loses the original traffic source)

    Thanks

  10. Ace Says:

    Hi…

    Is this trick still working with the latest changes in the GA? I’m asking as I’m a newbie when it comes to these things and might ask someone to do it for me. But, if this can be done or is still working to date, I might try it myself.

    Thanks

  11. Robert Kingston Says:

    One of those essential GWO posts, Ophir. I’m implementing this right now in fact.

    Anyway, just noticed the wrong types of quote marks are being used here:

    _gaq.push([_setReferrerOverride,realreferrer]);

    WordPress auto formats them to look nice. Unfortunately nice quote marks don’t work in code.

  12. jena Says:

    Will this solution work for Internet Explorer?

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>