Trying something tricky with onclick="_gaq.push()" - Need Help!

Joined
May 17, 2015
Messages
276
Likes
379
Degree
1
Ok, so I'm setting up Google Analytics tracking for my affiliates and I'm trying to be tricky with tracking my Sidebar Adverts. As a result I'm stuck.

I'm following this way of setting it up
www.matthewwoodward.co.uk/tutorials/how-to-track-anything-with-google-analytics/

Essentially I'm following this format when tracking clicks.

onclick="_gaq.push(['_trackEvent', 'category', 'action', 'label']);"​

I replace the 3 values:
  • Category – The category/type of event, e.g. Affiliate Click
  • Action – Give the action a name, eg click (I use affiliate programme name here)
  • Label – Anything else you want to include (I use the page URL)
So track an affiliate link click for Amazon on a best widgets page (eg: https://mysite.com/best-widgets/) it would look like this

onclick="_gaq.push(['_trackEvent', 'Affiliate', 'Amazon', '/best-widgets/']);"
The full link would look something like this:

<a href="https://mysite.com/go/widgets" target="_blank" rel="nofollow" onclick="_gaq.push(['_trackEvent', 'Affiliate', 'Amazon', '/best-widgets/']);">Blue Wigets</a>
That by itself is simple.

However, I have some sidebar adverts that I also want to track and I'm looking to implement some code that autofils the 'label' section with the page URL it is displayed on.

The tracking code looks something like this

onclick="_gaq.push(['_trackEvent', 'Affiliate', 'Amazon', '<?php echo $_SERVER['REQUEST_URI']; ?>​
']);"

However, when I put it live and inspect the element, it has put <?php echo $_SERVER['REQUEST_URI']; ?> a text and not recognised the code.

OR it doesn't even recognize the final " at the end which means the link doesn't even show up :/

I'm using Wordpress. This is in the Main Sidebar. The link is in a Text widget.

I'm stumped.

Any help is appreciated.

Cheers
 
If it's coming out as literally "<?php" that means it needs to be hard coded into the php template, cannot use the "<?php" code in the manner you are attempting to. Text widget won't execute php code without modification. Try this: https://wploop.com/run-php-code-text-widget-wordpress/

My gut is telling me to tell you to not do this though, it seems like a potential security breach in the making.
 
Thanks @CCarter . That link is spot on. I'll look a little bit further into this tomorrow (getting late here). Cheers
 
You can use the window.location JS object to get the current URL, ie:

onclick="_gaq.push(['_trackEvent', 'Affiliate', 'Amazon',
window.location.href]);"

Also, are you not using Universal Analytics? The event tracking implementation you're using here is deprecated IIRC. Should be something like this if you're using UA:

onclick="ga('send', 'event', 'Affiliate', 'Amazon', window.location.href);"
 
You can use the window.location JS object to get the current URL, ie:

onclick="_gaq.push(['_trackEvent', 'Affiliate', 'Amazon',
window.location.href]);"

Also, are you not using Universal Analytics? The event tracking implementation you're using here is deprecated IIRC. Should be something like this if you're using UA:

onclick="ga('send', 'event', 'Affiliate', 'Amazon', window.location.href);"
Haha, yeah wow. Just looked into it, I am behind the times.

Looking like I'll be updating the code this weekend :tongue:
 
Back