Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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?

share|improve this question
    
Has your question been answered here? –  Christopher Feb 14 '11 at 19:50

2 Answers 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.

share|improve this answer
    
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

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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