Skip to content

Instantly share code, notes, and snippets.

@mohsinrasool
Created October 18, 2015 19:32
Show Gist options
  • Save mohsinrasool/4bcab3e5388726807d43 to your computer and use it in GitHub Desktop.
Save mohsinrasool/4bcab3e5388726807d43 to your computer and use it in GitHub Desktop.
To track the submission of gravity forms and generate an event through Google Tag Manager, you can utilize the following code, It adds the onclick event to the submit button of the gravity form. So, whenever the submit button is clicked an event is sent to Google Server. You need to do two things to use this code
function add_conversion_tracking_code($button, $form) {
$dom = new DOMDocument();
$dom->loadHTML($button);
$input = $dom->getElementsByTagName('input')->item(0);
if ($input->hasAttribute('onclick')) {
$input->setAttribute("onclick","ga('send', 'event', { eventCategory: 'GTM_Category', eventAction: 'GTM_Action', eventLabel: 'GTM_Label'});".$input->getAttribute("onclick"));
} else {
$input->setAttribute("onclick","ga('send', 'event', { eventCategory: 'GTM_Category', eventAction: 'GTM_Action', eventLabel: 'GTM_Label'});");
}
return $dom->saveHtml();
}
add_filter( 'gform_submit_button_1', 'add_conversion_tracking_code', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment