Vikram
Vikram

Reputation: 11

Tracking events (via google analytics) for hashtag routes in Backbone single page app

With the backbone.analytics.js I'm able to track pageviews, but I have trouble in tracking events bound to different pages in My Site.

Though the events are triggered from different pages, since there is a hash tag in the URL the path after hashtag is ignored completely. Hence all the events fall under home page '/' in google analytics dashboard.

As you can see in the above example track1 and track2 belong to page1 and page2 respectively, but both track1 and track2 fall under home page '/'.

What are the possible solutions on this?

Upvotes: 1

Views: 405

Answers (1)

Philip Walton
Philip Walton

Reputation: 30461

When your page initially loads and you create the tracker object, it stores a bunch of info about the current page (e.g. title, url, screen resolution, etc.).

Then, when you send a hit to track a page view or an event or whatever, it also sends all that page data along with it. This allows you to do reporting on events per page or e-commerce actions per user, etc.

If you're creating a single page app that updates the title and/or URL as the user navigates throughout the site, you'll have to also update that tracker object prior to sending new hits. You can do that via _gaq.push(['_set', 'page', '/new_url']); with ga.js or via ga('set', 'page', '/new_url')' using analytics.js.

Here is the documentation on the set method: https://developers.google.com/analytics/devguides/collection/analyticsjs/method-reference#set

And here's some more general information about when you should use set: https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#set

Note: both of these links apply to analytics.js (Universal Analytics) rather than ga.js (Classic Analytics) as that's the recommend tracking method, but the concepts apply to both.

Upvotes: 2

Related Questions