Skip to content

Instantly share code, notes, and snippets.

@EastSideCode
Created April 16, 2018 16:01
Show Gist options
  • Save EastSideCode/5ad89055895d1b7b2e6418c834917453 to your computer and use it in GitHub Desktop.
Save EastSideCode/5ad89055895d1b7b2e6418c834917453 to your computer and use it in GitHub Desktop.
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