Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I track which page a button was clicked on in Google Analytics

Status
Not open for further replies.

xsquareduk

Technical User
Jun 16, 2010
2
GB
Hi all,

I have a template for a website which is called on multiple pages. Each of these has hovering buttons on to say whether someone likes the page or dislikes it, which at the moment just pops up a JavaScript alert for testing.

I want to change this so that when someone clicks 'I like this' it sends to Google Analytics both the event that they LIKED the page, but also the URL of the page they liked. Obviously as this is called in a template, it can't just send the same thing for each page. In effect, I am asking how to craft the string in JavaScript to pass the URL to Google Analytics.

Everything I have seen for tracking downloads, etc makes you specify a path to track, like /downloads/file.pdf for example. I would be happy to track something in the manner of /likes/url and /dislikes/url if necessary.

Hope someone can help, thanks!
 
Some further info - here is part of a similar page which works, but as you can see it uses a hardcoded URL to pass to GA (/external/webportal):

<a href=" style="width: 74px; height: 74px;" onclick="javascript: pageTracker._trackPageview('/external/webportal');"><img alt="Web Portal" src="/IT/button.JPG" target="_blank" /></a>

If I could tell it to replace /external/webportal with /like/URL then that would be perfect!
 
Assuming your template is embedded into the pages, rather than loading them into the template, try using window.location.pathname to get the current page.

You can then concatenate that to the domain url if needed.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top