function exactmetrics_forms_record_impression(event) { exactmetrics_add_bloom_forms_ids(); var exactmetrics_forms = document.getElementsByTagName("form"); var exactmetrics_forms_i; for (exactmetrics_forms_i = 0; exactmetrics_forms_i < exactmetrics_forms.length; exactmetrics_forms_i++) { var exactmetrics_form_id = exactmetrics_forms[exactmetrics_forms_i].getAttribute("id"); var skip_conversion = false; /* Check to see if it's contact form 7 if the id isn't set */ if (!exactmetrics_form_id) { exactmetrics_form_id = exactmetrics_forms[exactmetrics_forms_i].parentElement.getAttribute("id"); if (exactmetrics_form_id && exactmetrics_form_id.lastIndexOf('wpcf7-f', 0) === 0) { /* If so, let's grab that and set it to be the form's ID*/ var tokens = exactmetrics_form_id.split('-').slice(0, 2); var result = tokens.join('-'); exactmetrics_forms[exactmetrics_forms_i].setAttribute("id", result);/* Now we can do just what we did above */ exactmetrics_form_id = exactmetrics_forms[exactmetrics_forms_i].getAttribute("id"); } else { exactmetrics_form_id = false; } } /* Check if it's Ninja Forms & id isn't set. */ if (!exactmetrics_form_id && exactmetrics_forms[exactmetrics_forms_i].parentElement.className.indexOf('nf-form-layout') >= 0) { exactmetrics_form_id = exactmetrics_forms[exactmetrics_forms_i].parentElement.parentElement.parentElement.getAttribute('id'); if (exactmetrics_form_id && 0 === exactmetrics_form_id.lastIndexOf('nf-form-', 0)) { /* If so, let's grab that and set it to be the form's ID*/ tokens = exactmetrics_form_id.split('-').slice(0, 3); result = tokens.join('-'); exactmetrics_forms[exactmetrics_forms_i].setAttribute('id', result); /* Now we can do just what we did above */ exactmetrics_form_id = exactmetrics_forms[exactmetrics_forms_i].getAttribute('id'); skip_conversion = true; } } if (exactmetrics_form_id && exactmetrics_form_id !== 'commentform' && exactmetrics_form_id !== 'adminbar-search') { __gtagTracker('event', 'impression', { event_category: 'form', event_label: exactmetrics_form_id, value: 1, non_interaction: true }); /* If a WPForms Form, we can use custom tracking */ if (exactmetrics_form_id && 0 === exactmetrics_form_id.lastIndexOf('wpforms-form-', 0)) { continue; } /* Formiddable Forms, use custom tracking */ if (exactmetrics_forms_has_class(exactmetrics_forms[exactmetrics_forms_i], 'frm-show-form')) { continue; } /* If a Gravity Form, we can use custom tracking */ if (exactmetrics_form_id && 0 === exactmetrics_form_id.lastIndexOf('gform_', 0)) { continue; } /* If Ninja forms, we use custom conversion tracking */ if (skip_conversion) { continue; } var custom_conversion_mi_forms = false; if (custom_conversion_mi_forms) { continue; } if (window.jQuery) { (function (form_id) { jQuery(document).ready(function () { jQuery('#' + form_id).on('submit', exactmetrics_forms_record_conversion); }); })(exactmetrics_form_id); } else { var __gaFormsTrackerWindow = window; if (__gaFormsTrackerWindow.addEventListener) { document.getElementById(exactmetrics_form_id).addEventListener("submit", exactmetrics_forms_record_conversion, false); } else { if (__gaFormsTrackerWindow.attachEvent) { document.getElementById(exactmetrics_form_id).attachEvent("onsubmit", exactmetrics_forms_record_conversion); } } } } else { continue; } } } function exactmetrics_forms_has_class(element, className) { return (' ' + element.className + ' ').indexOf(' ' + className + ' ') > -1; } function exactmetrics_forms_record_conversion(event) { var exactmetrics_form_conversion_id = event.target.id; var exactmetrics_form_action = event.target.getAttribute("miforms-action"); if (exactmetrics_form_conversion_id && !exactmetrics_form_action) { document.getElementById(exactmetrics_form_conversion_id).setAttribute("miforms-action", "submitted"); __gtagTracker('event', 'conversion', { event_category: 'form', event_label: exactmetrics_form_conversion_id, value: 1, }); } } /* Attach the events to all clicks in the document after page and GA has loaded */ function exactmetrics_forms_load() { if (typeof (__gtagTracker) !== 'undefined' && __gtagTracker) { var __gtagFormsTrackerWindow = window; if (__gtagFormsTrackerWindow.addEventListener) { __gtagFormsTrackerWindow.addEventListener("load", exactmetrics_forms_record_impression, false); } else { if (__gtagFormsTrackerWindow.attachEvent) { __gtagFormsTrackerWindow.attachEvent("onload", exactmetrics_forms_record_impression); } } } else { setTimeout(exactmetrics_forms_load, 200); } } /* Custom Ninja Forms impression tracking */ if (window.jQuery) { jQuery(document).on('nfFormReady', function (e, layoutView) { var label = layoutView.el; label = label.substring(1, label.length); label = label.split('-').slice(0, 3).join('-'); __gtagTracker('event', 'impression', { event_category: 'form', event_label: label, value: 1, non_interaction: true }); }); } /* Custom Bloom Form tracker */ function exactmetrics_add_bloom_forms_ids() { var bloom_forms = document.querySelectorAll('.et_bloom_form_content form'); if (bloom_forms.length > 0) { for (var i = 0; i < bloom_forms.length; i++) { if ('' === bloom_forms[i].id) { var form_parent_root = exactmetrics_find_parent_with_class(bloom_forms[i], 'et_bloom_optin'); if (form_parent_root) { var classes = form_parent_root.className.split(' '); for (var j = 0; j < classes.length; ++j) { if (0 === classes[j].indexOf('et_bloom_optin')) { bloom_forms[i].id = classes[j]; } } } } } } } function exactmetrics_find_parent_with_class(element, className) { if (element.parentNode && '' !== className) { if (element.parentNode.className.indexOf(className) >= 0) { return element.parentNode; } else { return exactmetrics_find_parent_with_class(element.parentNode, className); } } return false; } exactmetrics_forms_load();