21

As far as I know, canonical URLs are only recognized by search engines - a way to ensure that when a search engine crawls your page, no matter which URL got it there, all of the "link juice" points to one canonical URL. So on a DNN site when example.com/, example.com/Default.aspx, example.com/tabid/36/Default.aspx, example.com/home/tabid/36/Default.aspx are all URLS for the homepage, a search engine can compress them all into one listing in it's index rather than diluting the PageRank across several URLs.

My question is whether canonical URLs are recognized by Google Analytics, or if there is any other trick to keep that same home page from showing up as 5 or 6 different pages (URLs) in Analytics.

David Grenier
  • 1,221
  • 1
  • 10
  • 23

3 Answers3

30

Not recognized by default. But it's easy to setup GA to track the canonical urls when they are available.

instead of calling

_gaq.push(['_trackPageview']);

You can use:

var canonical_link;
try{
  canonical_link = jQuery('link[rel=canonical]').attr('href').split(location.hostname)[1] || undefined;
}
catch(e){
  canonical_link = undefined;
}
_gaq.push(['_trackPageview', canonical_link]);
Eduardo
  • 22,574
  • 11
  • 76
  • 94
  • 2
    Might want to add a closing brace to the try, although the resulting error is fitting punishment for those blindly copying and pasting code. – Alex Jan 23 '14 at 06:36
  • 1
    Just wondering - is this still the best way of doing it? I can imagine it might break tracking on campaign information (utm_*) style parameters. – nwaltham Feb 12 '14 at 12:05
1

From what I recall, Matt Cutts of Google does recommend using 301 Redirects proper instead of relying on canonicalising through the introduced meta-element, and I would certainly prefer it over adding yet more complexity even to that (for example, such as some contrived JavaScript to do the Analytics submission).

Bottom line, treat the disease and not the symptoms: look closer to home and get Analytics to respect your implementation instead of disrespectingly shoehorning into that system.

This might not be viable if you actually need one page to be accessible using multiple URLs, but in that case I would scarecly see the value in combining them in Analytics in the first place.

Xiong Chiamiov
  • 13,076
  • 9
  • 63
  • 101
Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • I think you make some good points here. I am looking at this due to legacy issues, and in our special case, an across the board change to a template gets you more bang for the buck than looking for individual pages to redirect. – nwaltham Feb 12 '14 at 12:25
  • If you use 301, then won't you lose the referer for GA? – Jonathan Aquino Dec 04 '15 at 00:06
  • How would you handle a page that accepts query params? – makstaks Dec 14 '20 at 17:07
0

Per https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#location

...
ga('create', 'YOUR ID', 'auto');
ga('set', 'page', 'http://yourdomain/foo');
ga('set', 'title', 'New Title'); //optional to change title too!
ga('send', 'pageview');
...

Note you need to use a fully qualified url (include your domain) otherwise GA will ignore it

Aaron Sherman
  • 3,789
  • 1
  • 30
  • 33
  • I think you meant to say 'location' instead of 'page' https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#location – Guy de Carufel Nov 30 '17 at 02:23