Last active
January 25, 2018 12:01
-
-
Save flistefliste/785e792c43443a730b2990933ff47008 to your computer and use it in GitHub Desktop.
Track Contact Form 7 submissions events in WordPress and send it to Google Analytics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @desc CF7 submission tracking. Trigger ga_send (Google Analytics events tracking) when submitting CF7 form. | |
* @return void | |
*/ | |
function trackCf7Submissions() | |
{ | |
?> | |
<script> | |
//get current languages if necessary | |
var current_language = document.documentElement.lang; | |
// set parameters here : | |
var params = [ | |
{ | |
"form_id" : 1264, | |
"action" : "envoi_formulaire_avis" | |
}, | |
{ | |
"form_id" : 2030, | |
"action" : "envoi_formulaire_rappel" | |
}, | |
{ | |
"form_id" : 53, | |
"action" : "envoi_formulaire_contact" | |
}, | |
{ | |
"form_id" : 2031, | |
"action" : "envoi_formulaire_documentation" | |
} | |
]; | |
/** | |
* @desc add event listener on CF7 submission. See https://contactform7.com/dom-events/ for more details | |
**/ | |
document.addEventListener( 'wpcf7mailsent', function( event ) { | |
for( var i=0; i<params.length; i++){ | |
if(event.detail.contactFormId == params[i].form_id){ | |
action = params[i].action; | |
} | |
} | |
ga('send', 'event', 'prospect', action); | |
}, false ); | |
</script> | |
<?php | |
} | |
add_action('wp_head', 'trackCf7Submissions'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment