-
AuthorPosts
-
March 18, 2019 at 1:08 pm #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
March 21, 2019 at 8:10 am #1081212Hey 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,
IsmaelMarch 21, 2019 at 1:10 pm #1081365Hey 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
March 22, 2019 at 2:03 pm #1081834Hi,
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 -
AuthorPosts
- You must be logged in to reply to this topic.