Question

I'm trying to track onclick event to track a button on my site but I'm not too familiar with PHP.

Here's an example of how the code looks like on my site:

<a href="<?php if($clang=='en') { echo "http://www.website.com/english/"; } 
else {
echo "http://www.website.com/news/"; } ?>" class="xyz_<?php echo $clang; ?>">News Button</a>

I need to add this code below to track when users click on the "News Button" image:

onClick="_gaq.push(['_trackEvent', 'banner', 'click', 'News Button']);"

Please help.

Thank you

Was it helpful?

Solution

<a href="<?php echo $clang == 'en' ? "http://www.website.com/english/" : "http://www.website.com/news/"; ?>" class="xyz_<?php echo $clang; ?>" onClick="_gaq.push(['_trackEvent', 'banner', 'click', 'News Button']);">News Button</a>

That would be the quick way to add it. Let me know if you need anything clarified. I condensed your php to be handled by a ternary operator. No need for that if statement the way it was.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top