Canonical URL in Analytics
Asked Answered
G

3

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.

Gipon answered 1/2, 2012 at 21:43 Comment(1)
I think you should mark the answer as the correct one..Homespun
U
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]);
Urtication answered 2/2, 2012 at 7:15 Comment(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.Enterotomy
Just wondering - is this still the best way of doing it? I can imagine it might break tracking on campaign information (utm_*) style parameters.Madelene
W
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.

Welcy answered 29/4, 2013 at 11:39 Comment(3)
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.Madelene
If you use 301, then won't you lose the referer for GA?Tactician
How would you handle a page that accepts query params?Babu
C
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

Corabella answered 24/10, 2016 at 16:47 Comment(1)
I think you meant to say 'location' instead of 'page' developers.google.com/analytics/devguides/collection/…Tickle

© 2022 - 2024 — McMap. All rights reserved.