Skip to content

Instantly share code, notes, and snippets.

@flistefliste
Last active January 25, 2018 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flistefliste/785e792c43443a730b2990933ff47008 to your computer and use it in GitHub Desktop.
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
<?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