A safe, simple alternative to GA's recommended _link function that breaks pages.
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
README.md
jQuery-GA-link.js

README.md

Better Google Analytics Cross-domain Tracking

A safe, simple alternative to GA's recommended _link function that breaks pages.

The Problem

Google Analytics uses first party cookies. As a result, sites that want to track across top-level domains (say, your-site.com to yoursite.shopify.com) aren't able to. Google Analytics provides a function called _link that works around this problem by appending the cookies from the first domain onto the URL when the user transfers from one site to another.

The recommended method here looks like:

<a href="https://your-ecommerce-site.com" onclick="_gaq.push(["_link", "https://your-ecommerce-site.com"]); return false">Give us your money</a>

Why this is destructive

What GA is doing here is completely interrupting your click action -- that's what return false means. It then, on its own, grabs the information from the link, appends the cookie information to the URL, and does a window.location redirect to that URL.

This means a few things:

  • If GA fails to load (the user blocks GA, GA's servers are down or slow to respond, etc), _gaq.push(["_link"]); just adds an array containing the string _link to the _gaq array, which is meaningless. Your user clicks the link, and nothing happens.
  • The user gets a diminished user experience; if they try to force the link to open in a new window, it won't.
  • If the site's GA isn't properly configured, it breaks.
  • You need to manually add this onclick handler to each link you wish to track, which is a maintenance nightmare.

The alternative

This plugin helps manage all of your cross-domain tracking code in one place, without any of the safety pitfalls listed above.

  • Fails silently if GA is blocked or fails to load; links continue to work just normally.
  • Doesn't interrupt or override a user's click.

Usage

Here's an example usage. You have 3 domains, and you'd like the traffic between the 3 domains to be tracked seamlessly for any link between them. So, you'd do:

    $("a,area").filter("[href^='http']")._link(["yourdomain1.com", "yourdomain2.com", "yourdomain3.com"]);

I don't want to use GA's _link, but I don't need jQuery and your plugin is too complex

You can still use an onclick handler to do your cross domain tracking. It would look like this:

onclick="if(window._gat){this.href= _gat._getTrackerByName()._getLinkerUrl(this.href);};"

This:

  • Checks if window_gat is set. This variable only gets set when ga.js had loaded.
  • Sets the href of the link to be the "linker URL" version of itself.
  • Doesn't interrupt the user experience.