Śledzenie adresów z hashtagami

Źródło: http://stackoverflow.com/questions/4811172/is-it-possible-to-track-hash-links-like-pages-with-google-analytics

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?

Are you building entire websites in a single page too? It’s super user friendly and quick, I just love it! – Dave Jan 26 ’11 at 23:14
@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 – Markasoftware Nov 26 ’13 at 2:20

up vote 85 down vote accepted

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('send', 'pageview', {
 'page': location.pathname + location.search  + location.hash
});

This is how you’d do it if you’re using Google Analytics withing Google Tag Manager:

  • Go to your macros
  • Updated the URL Macro to „Fragment”
shareimprove this answer
perfect! … 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. – Dave Jan 27 ’11 at 20:37
2
You 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/… – Yahel Jan 27 ’11 at 20:43
Do you guys know how to do this with google analytics universal code? – KingOptimizer Mar 11 ’14 at 5:53
@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? – KingOptimizer Mar 12 ’14 at 0:21
1
This 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/… – natevw Dec 22 ’15 at 21:16

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 🙂

shareimprove this answer
5
This 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. – Yahel Jan 27 ’11 at 6:03

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”

shareimprove this answer

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’

shareimprove this answer

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'});
Reklamy

Skomentuj

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Wyloguj / Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj / Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj / Zmień )

Zdjęcie na Google+

Komentujesz korzystając z konta Google+. Wyloguj / Zmień )

Connecting to %s