Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Goal Tracking Master
// Include the Google Analytics Tracking Code
function google_analytics_tracking_code(){ ?>
<!-- script from analytics starts here -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-68863234-2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-68863234-2');
</script>
<!-- script from analytics ends here -->
<?php
}
add_action('wp_footer', 'google_analytics_tracking_code');
// add tracking for phone call clicks
function google_phone_clicks_and_contact_form() {
$goalPath = str_replace(home_url(),'',get_permalink());
?>
<!-- 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");
}
});
console.log("it was sent, but not known if it was clicked");
} // end if variable defined
}); // end click function
jQuery("a[href^='mailto']").click(function(event) {
if (typeof gtag !== 'undefined') {
gtag('event', 'Click', {
'event_category': 'Contact',
'event_label': 'Email',
'event_callback': function() {
console.log("email tracking sent successfully");
}
});
} // end if variable defined
}); // end click function
}); // end document ready
document.addEventListener( 'wpcf7mailsent', function( event ) {
if (typeof gtag !== 'undefined') {
gtag('event', 'Submit', {
'event_category': 'Contact Form',
'event_label': '<?php echo isset($goalPath) ? $goalPath : ""; ?>',
'event_callback': function() {
console.log("contact form tracking sent successfully");
}
});
} // end if variable defined
}, false );
</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
You can’t perform that action at this time.