Patrick
Patrick

Reputation: 2781

Google Analytics tracking code for domain with and without the WWW

I would like to know if this is the correct way to track the traffic of my .Net MVC website with Google Analytics if the access is from:

http://website.com/ControllerName
http://www.website.com/ControllerName

The Tracking Code:

(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-XXXXXXXX-Y', 'website.com/ControllerName');
ga('create', 'UA-XXXXXXXX-Y', 'www.website.com/ControllerName');

ga('send', 'pageview');

Upvotes: 0

Views: 131

Answers (2)

Shantanu
Shantanu

Reputation: 692

Use google tag manager instead of google analytics and you can create tags which would fire your page view when link contains "website.com".

Upvotes: 1

Eike Pierstorff
Eike Pierstorff

Reputation: 32760

Not really. This will create two trackers, and the second one will overwrite the first one (if it works at all, since the third parameter is in the wrong format - a domain name without path is expected). The third parameter is the domain on which the GA cookie ist set. If you set it to www.website.com GA will not be able to track visits on website.dom without www.

However you do not need special code to track pages with/without www (or other subdomains), simply set the third parameter to "auto" (unless versions with and without www display different things). You can set "hostname" as secondary dimension in your content reports to see if a page has been called on the www or non-www version.

Better yet (for SEO, Google Analytics is not affected by this) redirect all traffic to the same hostname.

Upvotes: 2

Related Questions