From 0a9e5de529b0671013d81c5799686f2012a5c5b9 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Tue, 17 Dec 2024 11:41:29 +0200 Subject: [PATCH 1/7] Simplify --- assets/js/controls.js | 90 +++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 58 deletions(-) diff --git a/assets/js/controls.js b/assets/js/controls.js index 6755a80..8fe7f8b 100644 --- a/assets/js/controls.js +++ b/assets/js/controls.js @@ -1,74 +1,39 @@ /* eslint camelcase: warn */ ( function( $ ) { - var jQueryEvent, formEventCallback; var formEventCallbacks = { - wpcf7mailsent: function( form ) { - var formConfig; - - if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) { - formConfig = getFormConfig( form.contactFormId ); - trackAnalyticsEvent( 'Contact Form', 'Sent', formConfig.title ); - } + wpcf7mailsent: function( form, formConfig ) { + trackAnalyticsEvent( 'Contact Form', 'Sent', formConfig.title ); }, - wpcf7mailfailed: function( form ) { - var formConfig; - - if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) { - formConfig = getFormConfig( form.contactFormId ); - trackAnalyticsEvent( 'Contact Form', 'Error', formConfig.title ); - } + wpcf7mailfailed: function( form, formConfig ) { + trackAnalyticsEvent( 'Contact Form', 'Error', formConfig.title ); }, - wpcf7spam: function( form ) { - var formConfig; - - if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) { - formConfig = getFormConfig( form.contactFormId ); - trackAnalyticsEvent( 'Contact Form', 'Spam', formConfig.title ); - } + wpcf7spam: function( form, formConfig ) { + trackAnalyticsEvent( 'Contact Form', 'Spam', formConfig.title ); }, - wpcf7submit: function( form ) { - var formConfig; + wpcf7submit: function( form, formConfig ) { + var errorStati = [ 'validation_failed', 'acceptance_missing', 'spam', 'aborted', 'mail_failed' ]; - if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) { - formConfig = getFormConfig( form.contactFormId ); + if ( form.status && -1 === errorStati.indexOf( form.status ) ) { trackAnalyticsEvent( 'Contact Form', 'Submit', formConfig.title ); + } else { + trackAnalyticsEvent( 'Contact Form', form.status, formConfig.title ); } - if ( form.contactFormId && 'mail_sent' === form.status && formEventEnabled( form.contactFormId, 'redirect-success' ) ) { - formConfig = getFormConfig( form.contactFormId ); - - if ( formConfig.redirect_url ) { - window.location = formConfig.redirect_url; - } + if ( 'mail_sent' === form.status && formEventEnabled( form.contactFormId, 'redirect-success' ) && formConfig.redirect_url ) { + window.location = formConfig.redirect_url; } } }; - var jQueryEvents = { - 'wpcf7:mailsent': function( event, form ) { - formCallbacks.wpcf7mailsent( form ); - }, - 'wpcf7:mailfailed': function( event, form ) { - formCallbacks.wpcf7mailfailed( form ); - }, - 'wpcf7:spam': function( event, form ) { - formCallbacks.wpcf7spam( form ); - }, - 'wpcf7:submit': function( event, form ) { - formCallbacks.wpcf7submit( form ); - } - }; - function trackAnalyticsEvent( eventCategory, eventAction, eventTitle ) { // Helper method required for the event to be registered by gtag.js. var dataLayerPush = function() { - if ( 'object' === typeof window.dataLayer && 'function' === typeof window.dataLayer.push ) { - window.dataLayer.push( arguments ); - } + window.dataLayer = window.dataLayer || []; + window.dataLayer.push( arguments ); }; // GA via Google Tag Manager or Global Site Tag (gtag.js). @@ -137,17 +102,26 @@ // Register the new JS events in CF7 version 5.2 and above. if ( 'function' === typeof document.addEventListener ) { for ( formEventCallback in formEventCallbacks ) { - document.addEventListener( formEventCallback, function( event ) { - if ( event.type in formEventCallbacks ) { - formEventCallbacks[ event.type ].call( event, event.detail ); + document.addEventListener( + formEventCallback, + function( event ) { + if ( event.type in formEventCallbacks && formEventEnabled( event.detail.contactFormId, 'track-ga' ) ) { + formEventCallbacks[ event.type ].call( event, event.detail, getFormConfig( event.detail.contactFormId ) ); + } } - } ); + ); } - - // Register the legacy jQuery events pre CF7 version 5.2. - } else if ( 'function' === typeof $ ) { + } else if ( 'function' === typeof $ ) { // Register the legacy jQuery events pre CF7 version 5.2. for ( jQueryEvent in jQueryEvents ) { - $( document ).on( jQueryEvent, jQueryEvents[ jQueryEvent ] ); + $( document ).on( + jQueryEvent, + function( event ) { + var eventType = jQueryEvent.replace( ':', '' ); + if ( formEventCallbacks[ eventType ] && formEventEnabled( event.details.contactFormId, 'track-ga' ) ) { + formEventCallbacks[ eventType ].call( event.details ); + } + } + ); } } From dde1df8edaad073a1bd32390815667bc5d4deb62 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Tue, 17 Dec 2024 11:43:57 +0200 Subject: [PATCH 2/7] Place our storage link at the top --- src/class-cf7-extras.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/class-cf7-extras.php b/src/class-cf7-extras.php index e14182e..bf45cdc 100644 --- a/src/class-cf7-extras.php +++ b/src/class-cf7-extras.php @@ -348,12 +348,6 @@ public function wpcf7_metabox( $cf7 ) { ); } - // Place the storage links on top. - $fields = array_merge( - array( 'extra-cf7-storage' => $storage_field ), - $fields - ); - /** * Let plugins add items to the settings. * @@ -361,6 +355,12 @@ public function wpcf7_metabox( $cf7 ) { */ $fields = apply_filters( 'cf7_extras__controls_fields', $fields, $settings ); + // Place the storage links on top. + $fields = array_merge( + array( 'extra-cf7-storage' => $storage_field ), + $fields + ); + $rows = array(); foreach ( $fields as $field_id => $field ) { From 908bdcaff5ad019b9855276ac675d4d9a43d692b Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Tue, 17 Dec 2024 11:45:41 +0200 Subject: [PATCH 3/7] The legacy version with jQuery already defined it as self dependency --- src/class-cf7-extras.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/class-cf7-extras.php b/src/class-cf7-extras.php index bf45cdc..6dcee40 100644 --- a/src/class-cf7-extras.php +++ b/src/class-cf7-extras.php @@ -622,7 +622,7 @@ public function track_form_events() { wp_enqueue_script( 'cf7-extras', $this->asset_url( 'assets/js/controls.js' ), - array( 'contact-form-7', 'jquery' ), + array( 'contact-form-7' ), self::ASSET_VERSION, true ); From e4fefbfc5f06b99f6423dc3b19541e2a379327f5 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Tue, 17 Dec 2024 11:49:54 +0200 Subject: [PATCH 4/7] Remove legacy jQuery support --- assets/js/controls.js | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/assets/js/controls.js b/assets/js/controls.js index 8fe7f8b..b0432fc 100644 --- a/assets/js/controls.js +++ b/assets/js/controls.js @@ -1,7 +1,7 @@ /* eslint camelcase: warn */ -( function( $ ) { - var jQueryEvent, formEventCallback; +( function() { + var formEventCallback; var formEventCallbacks = { wpcf7mailsent: function( form, formConfig ) { @@ -111,18 +111,6 @@ } ); } - } else if ( 'function' === typeof $ ) { // Register the legacy jQuery events pre CF7 version 5.2. - for ( jQueryEvent in jQueryEvents ) { - $( document ).on( - jQueryEvent, - function( event ) { - var eventType = jQueryEvent.replace( ':', '' ); - if ( formEventCallbacks[ eventType ] && formEventEnabled( event.details.contactFormId, 'track-ga' ) ) { - formEventCallbacks[ eventType ].call( event.details ); - } - } - ); - } } -}( jQuery ) ); +}() ); From 70909be076638ee5808f2874fc0130a686daa20e Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Tue, 17 Dec 2024 11:51:03 +0200 Subject: [PATCH 5/7] No need --- assets/js/controls.js | 1 - 1 file changed, 1 deletion(-) diff --git a/assets/js/controls.js b/assets/js/controls.js index b0432fc..c9c450c 100644 --- a/assets/js/controls.js +++ b/assets/js/controls.js @@ -1,5 +1,4 @@ /* eslint camelcase: warn */ - ( function() { var formEventCallback; From ee63ad216cacc6262c662b15d3a83c20c6cd62fe Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Tue, 17 Dec 2024 12:01:16 +0200 Subject: [PATCH 6/7] Add sample script for debugging during development --- readme.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/readme.md b/readme.md index a509b0c..26aa402 100644 --- a/readme.md +++ b/readme.md @@ -41,3 +41,45 @@ We use [Composer](https://getcomposer.org) for managing PHP development dependen ## Screenshot ![Contact Form 7 Controls](screenshot-1.png) + +## Sample Analytics Scripts + +Note: all scripts use a fake account ID `abc123`. + +Google Tag Manager (GTM): + +```html + +``` + +Google Analytics 4 (gtag.js): + +```html + + +``` + +Facebook Pixel: + +```html + +``` From 340fa8d688f1c02a5b7814262f093dc9349b4f3e Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Tue, 17 Dec 2024 12:07:36 +0200 Subject: [PATCH 7/7] It needs the protocol --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 26aa402..5a0bed3 100644 --- a/readme.md +++ b/readme.md @@ -52,7 +52,7 @@ Google Tag Manager (GTM): ```