Tracking links within your site with Google Analytics
“I have this banner I’m putting on my homepage to promote a particular product. How do I tell how many people clicked on that banner to get to the product page?”
People ask me this kind of question, or variations thereof, all the time. It’s all about understanding how often some particular link on your site is getting clicked, or differentiating between two or more different links to the same page.
Sometimes people ask if they should use campaign codes to track these kinds of links. That’s the right instinct, but I recommend against using campaign codes. The reason is simple: if a visitor clicks a link with campaign codes, they’ll overwrite the visitor’s previous referral information — that is, how they actually got to your site in the first place.
How to differentiate links
OK, so campaign codes are out. What to do?
The answer is simple, and related to the way campaign codes work. You’ll recall that “query parameters” are those things after a question mark (?) in a URL. The great part about them is, if your web server doesn’t recognize them, it just ignores them. So for example, this URL:
http://www.example.com/mypage.html
is identical to this one, as far as my web server is concerned:
http://www.example.com/mypage.html?from=banner
They’ll both take me to the same page. Here’s a real-life example you might be familiar with, the top menu from Facebook:
Both the logo and the “Home” link take you to your home page, but the URLs are slightly different:
http://www.facebook.com/home.php?ref=logo
http://www.facebook.com/home.php?ref=home
They’re using the “ref” query parameter to see which place you clicked. It’s the same with the “Profile” link and your name.
So the URLs go to the same pages, as far as the visitor is concerned. But if I’m using Google Analytics and take a look at the Top Content report, I’ll see separate listings. And whatever the query parameter is that I’m using (“from” in this case), I can easily use the “contains/excludes” filter at the bottom to find those pageviews.
Best practices
If you use this method, you’ll notice that the pageviews for the page in question are disaggregated according to the query parameter you used. This is great — it’s exactly what we’re after. But if you want the total pageviews for that page, you have to do some addition.
The best thing to do would be to have two profiles, one where you take the query parameter into account to disaggregate those links, and another where you strip the query parameter away to easily see total pageviews to each page, irrespective of which link someone clicked to get there.
Google Analytics lets you easily strip away query parameters you don’t want. In the profile settings, just enter in whatever query parameters you’d like to take out.
This will give you a profile with the aggregated pageviews, without your query parameter.




Have you thought about the consequences regarding SEO? A searchengine spider sees 2 URL’s with the same content on them. People can link to both URL’s and the pagerank will be divided. The solution is to do some user-agent cloaking to check whether a searchengine’s spider visits the site: show them the URL’s without the extra parameter. And if they land on a page with the extra parameter, do a 301 redirect to the URL without the extra parameter.
Yeah, I agree…the SEO implications of this would not be good since you would be creating duplicate content throughout your site. I would probably only give this advice to people that are testing things with paid traffic and have them robots.txt out any pages with query parameters.
It is one of the great limitations of Google Analytics that one cannot make use of parameters within a profile (well, except the onsite search one). One always has to create new profiles whenever a new question arises, although this is thankfully less true since GA made custom segmentation available.
What are your thoughts on launching a page view through an onclick event instead? You still need a seperate profile to view the information but at least you keep things nice and neat for search engines.
Steve — using an onclick event would be a nice solution that keeps the link tidy for search engines, but passes the information you want to GA. Of course, the downside is that you have to maintain the code for the onclick events, but if you’re already tagging the links with query parameters to differentiate them anyway, it’s not significantly more effort.
I need to get with the program and explore Google analytics more.
We had ill-advisably used the “source/medium/campaign” tracking variables for our homepage site links. This had in fact overwrote the previous referral info as suggested in this article. We are now going back and creating a unique variable and will use the filter concept in analytics to understand content popularity and/or impact. Found this to be very helpful. Thanks!
I did it exactly like this post suggests for a while. The seo issues are not that hard if you use canonical tags, but the difficult addition and constant switching between at least two necessary profiles made me drop it altogether though. We use event tracking now. That keeps the urls clean. It has a downside too though: it is much harder for example to compare the paths of users that arrived at a page via a banner on the homepage with the path of those that came there via another path.
In any case: you can do it the way lunametrics describes. But you have to be really careful with your profile setup and use canonical tags. If a lot of unexperienced users use your google analytics, it may be better to keep the URL reports clean by using event tracking.
Btw: the event tracking was a recommendation of avinash kaushik.