Search code examples
google-analyticsevent-tracking

Google Analytics just for event tracking?


What i'm trying to build out is a html page that will be included on a partner site. The idea is we give the partner a JS file that then renders the html. We don't want to use an iFrame, so the new HTML would just be a with links and copy.

We want to track link usage, and i though if i added a GA tracking snippet i could use it just for event tracking. I want to avoid getting page view data from the partner site.


Solution

  • If you remove the line ga('send', 'pageview') from the JavaScript tracking snippet, then you won't track any pageviews.

    The normal tracking snippet looks like this:

    <!-- Google Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    
    ga('create', 'UA-XXXXX-Y', 'auto');
    ga('send', 'pageview');
    </script>
    <!-- End Google Analytics -->
    

    To not track pageviews, just remove the last line like so:

    <!-- Google Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    
    ga('create', 'UA-XXXXX-Y', 'auto');
    </script>
    <!-- End Google Analytics -->