Okay
  Public Ticket #807570
Google Analytics Event Tracking
Closed

Comments

  • Phil started the conversation

    How to sent onClick events to Google Analytics? I have the analytics js pasted into the Footer option in the theme config. 

    I have tried this: <a onClick="ga('send', 'event', 'Link', 'Click', 'App', '0');" href="url"> and onclick="__gaTracker( ... " to no avail. 

    My preference would be not to use another plugin. 

  • [deleted] replied

    Hi Phil,

    You can do this with jQuery,

    <script type="text/javascript">
    jQuery( document ).ready( function( $ ) {
        // Bind click events
        jQuery( 'a' ).on( 'click', function( e ) {
            ga('send', 'event', 'Link', 'Click', 'App', '0');
        } );
    } );
    </script>

    Maybe this could work for you?

  • Phil replied

    Hi -- Thanks. Could I add this to the footer field in themes.php?page=theme-options#footer-custom-js

    I would like the flexibility to  pass different values to the event depending on the button/link. Or have the script pass the label of the link/button to Google. Is this possible?

  • [deleted] replied

    Hi Phil,

    It depends what information you want to pass and which links do you target, for example you can pass the Link Name (value) in this form:

    <script type="text/javascript">
    jQuery( document ).ready( function( $ ) {
    // Bind click events
    jQuery( 'a' ).on( 'click', function( e ) {         var name = $( this ).text(); // Pass the "name" variable         
    ga('send', 'event', 'Link', 'Click', 'App', '0');
    } );
    } );
    </script>
  • Phil replied

    Where do I add this script. To the top of each portfolio post. Or to the footer? Or in header.php? 

  • [deleted] replied

    Hi Phil,

    You need to place that code on Theme Options > FooterJavaScript & Tracking Code > Footer JavaScript