Skip to content

Instantly share code, notes, and snippets.

@EastSideCode
Last active March 8, 2018 12:53
Show Gist options
  • Select an option

  • Save EastSideCode/9d21f704b1f673bc30475f624a27f346 to your computer and use it in GitHub Desktop.

Select an option

Save EastSideCode/9d21f704b1f673bc30475f624a27f346 to your computer and use it in GitHub Desktop.
Phone tracking using gtag
// add tracking for phone call clicks and contact form
function google_phone_clicks_and_contact_form() { ?>
<!-- phone call tracking for analytics -->
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("a[href^='tel:']").click(function(event) {
if (typeof gtag !== 'undefined') {
gtag('event', 'Click', {
'event_category': 'Contact',
'event_label': 'Phone',
'event_callback': function() {
console.log("information sent successfully");
}
});
} // end if variable defined
}); // end click function
}); // end document ready
<?php if (is_page(23)) : // if on the contact page ?>
document.addEventListener( 'wpcf7mailsent', function( event ) {
console.log("form submitted");
gtag('event', 'Submit', {
'event_category': 'Contact',
'event_label': 'Contact Form',
'event_callback': function() {
console.log("contact form tracking sent successfully");
}
});
}, false );
<?php endif; ?>
</script>
<?php
}
// include in footer
add_action('wp_footer', 'google_phone_clicks_and_contact_form');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment