: How to track subdomains with Google Analytics while having mod_rewrite redirect to a subdomain? When users come directly to domain.com or www.domain.com, I am redirecting them to shop.domain.com
When users come directly to domain.com or 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 ^(.*)$ 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?
More posts by @Goswami781
3 Comments
Sorted by latest first Latest Oldest Best
The following example describes how to keep the referrer while switching from http to https.
In your .htaccess:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ %{SERVER_NAME}/?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');
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.
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:
www.roirevolution.com/blog/2011/01/google_analytics_subdomain_tracking.php
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=(.*)/,''));
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 ^(.*)$ shop.domain.com/?from=%1 [R=301,QSA,L]
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ shop.domain.com/ [R=301,L]
You will need to repeat the above for domain.com as well.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.