Colby Whitted
Colby Whitted

Reputation: 69

Google analytics event tracking not working. Tracking Contact form 7 and dynamically generated element

Google analytics is working properly on my site for most things. However I have implemented event tracking on two different elements and after testing, GA is not showing the events occur. Here is my tracking code:

<script async src='//www.google-analytics.com/analytics.js'></script>
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-xxxxxxxx-1', 'auto');
ga('send', 'pageview');
</script>

The first element I am attempting to track is an element I have generated and inserted with Javascript:

$(".tencol.last").append(" <p class='mobile-number'><a class='mobile-number-link' href='tel:555-555-5555'>(555) 555-5555</a></p>");

document.getElementsByClassName("mobile-number-link")[0].setAttribute( "onClick", "ga.push(['_trackEvent', 'mobile-call', 'mobile-phone-number-click']);");

The 2nd element I am attempting to track is a "Contact Form 7" contact form. I am using the built in 'Additional settings' on the "Contact Form 7" contact form editor page. This is the code I am using in that box:

on_sent_ok: "ga.push(['_trackEvent', 'Contact-Form', 'Submit'])";
on_sent_ok: "location.replace('http://www.(mydomain).com/?page_id=740');"

Thank you in advance for the helpful answers

Upvotes: 0

Views: 675

Answers (1)

nyuen
nyuen

Reputation: 8907

You are mixing Universal Analytics syntax and Classic GA syntax. It looks like your tracking is using the former, but the way you are sending in the events is based on the latter. Your event tracking code should be:

ga('send', 'event', 'mobile-call', 'mobile-phone-number-click'); // for the first element
ga('send', 'event', 'Contact-Form', 'Submit'); // for the second element

Upvotes: 1

Related Questions