Using Google Analytics to track clicks on a hotspot

  • 1
  • Idea
  • Updated 1 year ago
I am using Joomla to host the smartplayer on my site. I need to track the clicks on a video hotspot which links to an external URL with Google Analytics. To do this, you can use a simple trick. Point your hotspot link to a PHP page on your site (e.g. "Redirect_From_Hotspot1.php")

Create a PHP page with the following content. Replace the URL with your final URL and use your Google UA-xxxxxxx-x code.

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://my-link.com

echo ("<script type=\"text/javascript\">
    window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
    ga('create', 'UA [PUT YOUR GOOGLE CODE HERE]', 'auto');
    ga('send', 'pageview');
    ga('require', 'displayfeatures');
    ga('set', 'anonymizeIp', true);
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>;
<script type=\"text/javascript\">
    var gaProperty = 'UA-[PUT YOUR GOOGLE CODE HERE]'';

    var disableStr = 'ga-disable-' + gaProperty;
    if (document.cookie.indexOf(disableStr + '=true') > -1) {
      window[disableStr] = true;
    }

    function gaOptout() {
      document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
      window[disableStr] = true;
    }

    ga('send','event', 'ANALYTICS 'CATEGORY, 'ANALYTICS ACTION', 'ANALYTICS LABEL');
</script>");

exit();
?>

Hope that helps :)
Photo of dworatzek

dworatzek

  • 2 Posts
  • 0 Reply Likes

Posted 1 year ago

  • 1
Photo of dworatzek

dworatzek

  • 2 Posts
  • 0 Reply Likes
Sorry, the editor killed a part of a line, it should read:

header("Location: http://my-link.com");