How to exclude payment method as referral in Googl

2019-03-04 14:28发布

问题:

My payment gateways show up as referrals in Google Analytics. I've read a bit about it, and it looks like there are two options to fix this:

  1. Adding an entry to the Referral Exclusion List in Google Analytics admin page. (works with Google Universal Analytics)
  2. Attach utm_nooverride=1 to the URL of the payment gateway return page. Apparently this is not a solution for Google Universal Analytics.

This article does a good job of explaining the issue.

I would like to know if there is another alternative, because I am managing 600+ Analytics accounts that need this fix, and manually updating the Referral Exclusion List for each account will take a lot of time. I need an alternative similar to option #2, where I can have a URL parameter or where I can send some kind of settings to analytics.js just before it tracks the pageview, telling it to ignore the referral.

回答1:

No idea if this has any side effects, but the following seems to work: Universal Analytics has a "set"-method that allows you to override fields before they are send to the server. So you can override the referrer field and set it to some domain that's already part of your referral exclusion list.

So if you want to keep the utm_nooverride parameter you could do something like:

(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-XX', 'auto');
if(location.search.indexOf('nooverride') > -1) {
    ga('set', 'referrer', 'yourdomain.com');
}
ga('send', 'pageview');

If the query string contains "nooverride" (of course you can choose any other name for the parameter) the referrer is set to the name of your domain, and your domain is (or at least should be) already part of your referral exclusion list.

You might want to test this more extensively, but it worked for me when I whipped up a test page for this question so it should be worth exploring.