4

When users come directly to domain.com or www.domain.com, I am redirecting them to shop.domain.com via this .htaccess rewrite:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^domain.com$ 
RewriteRule ^(.*)$ http://shop.domain.com/ [R=301,L]

The content served by shop.domain.com has the following tracking code parameters:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-123456-6']);
_gaq.push(['_setDomainName', '.domain.com']);
_gaq.push(['_trackPageview']);

All direct visits that come to shop.domain.com as a result of the rewrite from domain.com are tracked as referral traffic, showing my own domain.com as referral source in Google Amalytics.

I would like to track these visits as direct traffic.

How to change the configuration to track mod_rewritten traffic on my subdomain coming from my own domain as direct traffic?

  • Has your question been answered here? – Christopher Feb 14 '11 at 19:50
3

Instead of overriding the referrer information, you should add the subdomain to the Referrer Ignore List that Google Analytics stores.

You can do this using the _addIgnoredRef() method in the GA API.

Google's description of the the method:

_addIgnoredRef()

Excludes a source as a referring site. Use this option when you want to set certain referring links as direct traffic, rather than as referring sites. For example, your company might own another domain that you want to track as direct traffic so that it does not show up on the "Referring Sites" reports. Requests from excluded referrals are still counted in your overall page view count.

http://code.google.com/apis/analytics/docs/gaJS/gaJSApiSearchEngines.html#_gat.GA_Tracker_._addIgnoredRef

Note: the examples given by Google may not be 100% correct. Check out the following link for a detailed discussion and example implementation:

http://www.roirevolution.com/blog/2011/01/google_analytics_subdomain_tracking.php

2

You can override the referrer that Google Analytics is using, read more in the documentation for _setReferrerOverride.

To somehow provide this correct referrer, you need to pass it via the 301 redirect. I can suggest adding a query parameter to your redirected url, and writing a javascript snippet that will extract this information from the document.location.

An example of adding javascript to your GA code:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
var _from = unescape(window.location.href.replace(/.*[?&]from=(.*)/,'$1'));
if (_from && _from.length > 0) {
  _gaq.push(['_setReferrerOverride',_from]);
}
_gaq.push(['_trackPageview']);

And, an example of using mod_rewrite to transfer the referrer, if a referrer exists:

RewriteCond %{HTTP_HOST} ^domain.com\.com$ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} (.*)
RewriteRule ^(.*)$ http://shop.domain.com/$1?from=%1 [R=301,QSA,L]

RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://shop.domain.com/$1 [R=301,L]

You will need to repeat the above for www.domain.com as well.

  • disclaimer: even though I tried to test this as best as I can, it may still not work 100%. so I leave the testing up to the users of the suggested above. – Evgeny Oct 30 '10 at 23:58
  • Thank you for the suggestion Evgeny. Even though this seems to work, I would not like to add URL parameters when performing the redirection. I would prefer a method that would not alter user experience in any way. – Marek Oct 31 '10 at 19:59
  • well, you can improve the above be redirecting to a url with a hash (#) instead of a query param. and you can remove hashes using javascript without the page reloading in the browser. – Evgeny Oct 31 '10 at 20:03
2

The following example describes how to keep the referrer while switching from http to https.

In your .htaccess:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1?referrer=%{HTTP_REFERER} [L,QSA,R=301]

The Google Analytics call:

(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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-xxx-y', 'auto');
ga('set', 'anonymizeIp', true);
try {
  var match = /.*[?&]referrer=(.*)/.exec(window.location.href);
  if (match && match.length > 1) {  
      var _from = decodeURIComponent(match[1]);
      if (_from && _from.length > 0) {
          console.log('Override referrer:', _from);
          ga('set', 'referrer', _from);
      }
  }
} catch (err) { ; }
ga('send', 'pageview');
  • 1
    There should be no space in the RewriteRule flags. ie. [L, QSA,R=301] should be [L,QSA,R=301] (otherwise, you'll get a 500 error). – MrWhite Aug 2 '17 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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