Tagged: , ,

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1079864

    Hi all,

    My question isn’t really theme related but if someone can answer I’ll be eternally grateful.
    I’ve seen different ways of adding Google event tracking to buttons but none of them work.
    Searching the web I found some that did….

    function superpowers_script_event(){
    ?>
    <script>
    jQuery('.powers-btn-1 a').click(function(){	
    	var buttonText = jQuery(this).text();
    	ga('send', 'event', { eventCategory: 'superpowers', eventAction: 'download', eventLabel: 'joinedup', eventValue: 1, buttonText});
    });
    </script>
    <?php
    }
    add_action('wp_footer', 'superpowers_script_event');

    I don’t get fully jQuery but I understand enough to tweak and get things working.

    2 Questions

    Is the var buttonText line needed? (what does it even do) Could the script just read…

    <script>
    jQuery('.powers-btn-1 a').click(function(){
    	ga('send', 'event', { eventCategory: 'superpowers', eventAction: 'download', eventLabel: 'joinedup', eventValue: 1, buttonText});
    });
    </script>

    How could I add more events to that code? So a different event for ,powers-btn-2, btn-3 and so on rather than replicate the entire thing

    The code does work so no immediate rush for an answer.

    Thanks guys/gals

    TJ

    #1081212

    Hey TJ,

    Thanks for the update.

    I’m not sure why they added it there because the last field should be the eventValue. You can remove the buttonText variable, but make sure that it’s also removed from the ga function.

    ga('send', 'event', { eventCategory: 'superpowers', eventAction: 'download', eventLabel: 'joinedup', eventValue: 1});
    

    Best regards,
    Ismael

    #1081365

    Hey Ismael,

    I’ll remove the var statement part and see what happens.

    Is there a way to and more click functions to that code for other buttons? like below..

    function superpowers_script_event(){
    ?>
    <script>
    jQuery('.powers-btn-1 a').click(function(){	
    	ga('send', 'event', { eventCategory: 'superpowers', eventAction: 'download', eventLabel: 'joinedup', eventValue: 1});
    });
    jQuery('.powers-btn-2 a').click(function(){	
    	ga('send', 'event', { eventCategory: 'superpowers', eventAction: 'download', eventLabel: 'joinedup', eventValue: 1});
    });
    </script>
    <?php
    }
    add_action('wp_footer', 'superpowers_script_event');

    I only need to load one function then rather than multiple. Would the above work? Is there a neater way to do it?
    Again i know this is way off theme support so no rush :)

    Thanks

    TJ

    #1081834

    Hi,

    Thanks for the update.

    You can combine the selectors if they are intended for a single event goal.

    function superpowers_script_event(){
    ?>
    <script>
    jQuery('.powers-btn-1 a, .powers-btn-2 a').on('click', function(){	
    	ga('send', 'event', { eventCategory: 'superpowers', eventAction: 'download', eventLabel: 'joinedup', eventValue: 1});
    });
    </script>
    <?php
    }
    add_action('wp_footer', 'superpowers_script_event');
    

    Best regards,
    Ismael

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.