Is it possible to track hash links like pages with google analytics?
Solution 1
Generically, your code could look like this
_gaq.push(['_trackPageview',location.pathname + location.search + location.hash]);
You could either bind that code to every time you have a hash change within your application, or you could use a generic hashchange plugin, that uses the HTML5 onhashchange, and some backwards compatible hacks for older browsers, and bind this code to that event, so that it fires every time your hash changes.
Using that plugin, your code could look like:
$(window).hashchange( function(){
_gaq.push(['_trackPageview',location.pathname + location.search + location.hash]);
})
**UPDATE 2014:**
This is how you'd do this in the new Universal Analytics:
ga('set', 'page', location.pathname + location.search + location.hash);
ga('send', 'pageview');
Note from Google Analytics documentation:
While technically the
send
command for pageview hits accepts an optionalpage
field as the third parameter, passing thepage
field that way is not recommended when measuring single page applications.
This is how you'd do it if you're using Google Analytics within Google Tag Manager:
- Go to your macros
- Updated the URL Macro to "Fragment"
Solution 2
Looks like this might be useful too: https://developers.google.com/analytics/devguides/collection/analyticsjs/single-page-applications
Very helpful with clear 'What to do' and 'What not to do'
Solution 3
Google Analytics allows you to track custom events, for example AJAX page loads.
(The usual caveats apply when doing this - hopefully there are non-javascript ways to access the same data :)
Solution 4
Good question. To track the hash link, you must track an event or a pageview, for every link to this hash. For the pageView, a sample code is below
onclick="_gaq.push(['_trackPageview','/page/hashLink1']);"
Note: This method create a virtual page view that is summing up to the count of the pages of your site. If your site is a big html files with anchors (maybe there is a slider to this page), this method gives you an estimated of the interaction of the user with your "content"
Solution 5
For new universal tracking this doesn't work anymore. You will have to go to https://developers.google.com/analytics/devguides/collection/analyticsjs/events and update to something like
ga('send', 'event', 'category', 'action', {'page': '/my-new-page'});
Dave
Updated on July 09, 2022Comments
-
Dave almost 2 years
Is it possible to track hash links like pages with google analytics?
For example, I want index.php/#1, index.php/#2, and index.php/#3 to all show up as individual page hits with individual time spent on page.
If there is no simple way of doing this, how can I add a track event to an onclick event with jquery? Can I still receive accurate time on "page" information this way?
-
Dave over 13 yearsAre you building entire websites in a single page too? It's super user friendly and quick, I just love it!
-
markasoftware over 10 years@Dave i wouldn't exactly call it "super user friendly"...im using it on my website just because there is no other way with the way my website works
-
Scott Weaver over 7 yearsI thought Dave was being sarcastic, until I realized he asked the question :)
-
-
Yahel over 13 yearsThis is not what Custom Events are for. They are for non-page actions. If your site uses hash changes to do what normal page loads do, you should use virtual page views. Custom Events are for tracking things that don't fit the traditional pageview paradigm.
-
Dave over 13 yearsperfect! ... as long as it works (which it should). I'll give you my pick here in a couple hours when I see hits coming in. I actually have a slide event happen on hash changes so I slapped it in there without any plugins.
-
Yahel over 13 yearsYou can see how it sends to your account live in Chrome with this great Extension. It's made by the Google Analytics team. chrome.google.com/extensions/detail/…
-
KingOptimizer about 10 yearsDo you guys know how to do this with google analytics universal code?
-
KingOptimizer about 10 years@Yahel - Thanks for your reply! I am sorry, I should have explained more. Google universal analytics is currently plugged into GTM. Do you know how I can adjust this using Google Tag Manager?
-
Nicolas Renon over 9 yearsA shorthand way could be
ga('send', 'pageview', location.pathname+location.search+location.hash);
if you don't need to specify any other parameters as the title. -
natevw over 8 yearsThis answer didn't make it clear if this was a "hack" or not, and the original way uses a bunch of underscore-prefixed stuff that's often a sign of unintentional use. But at least in the case of the new "Universal Analytics" snippet this is documented by Google: developers.google.com/analytics/devguides/collection/…
-
Yahel over 8 years@natevw Not a hack. GA's old global namespace was
_gaq
. The underscore was intended to reduce to possibility of a namespace collision with your own JavaScript on the page. -
jkupczak about 4 yearsFrom Google: "While technically the send command for pageview hits accepts an optional page field as the third parameter, passing the page field that way is not recommended when measuring single page applications. This is because fields passed via the send command are not set on the tracker—they apply to the current hit only. Not updating the tracker will cause problems if your application sends any non-pageview hits (e.g. events or social interactions), as those hits will be associated with whatever page value the tracker had when it was created."
-
ClosDesign about 3 yearsI kno wthis is way old, but if we send the ga('send', 'pageview', window.location.href), would it also send the hash or do you need to actually do the whole "location.hash" part of the URL?