Anagio
Anagio

Reputation: 3075

Insert variable into onClick event Google Analytics virtual pageview

I'm using js to get the URL of the current page which contains a hash mark because of AJAXs updates to the page

var hash = location.href;

A URL looks like http://www.example.com/#project1

What I need is this Google virtual pageview code to display the hash variable path only where it says hash here

onClick="_gaq.push(['_trackPageview', 'hash here']);"

to look like onClick="_gaq.push(['_trackPageview', '/#project1']);"

Thanks

Upvotes: 0

Views: 700

Answers (2)

tgormtx
tgormtx

Reputation: 322

Do this:

onClick="pushGA();"


function pushGA() {
     _gaq.push(['_trackPageview', (window.location.path + window.location.hash)]);
}

Or:

onClick="pushGA();"


function pushGA(path) {
     _gaq.push(['_trackPageview', ('/#'+window.location.hash)]);
}

Or:

onClick="pushGA('/#project');"


function pushGA(path) {
     _gaq.push(['_trackPageview', path]);
}

Upvotes: 0

mike
mike

Reputation: 7177

I think you're looking for location.hash...

function track() {
  _gaq.push(['_trackPageview', location.hash]);
}

Upvotes: 1

Related Questions