From 99cb44f0af1512aa73ecda47c616b296ec5aabf2 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Sun, 17 Sep 2017 00:14:36 +0300 Subject: [PATCH 01/10] Move tracking to JS --- js/controls.js | 66 ++++++++++++++++++++++++++++++++++++++++ plugin.php | 82 ++++++++++++++++++++++++++------------------------ 2 files changed, 108 insertions(+), 40 deletions(-) create mode 100644 js/controls.js diff --git a/js/controls.js b/js/controls.js new file mode 100644 index 0000000..48e4f09 --- /dev/null +++ b/js/controls.js @@ -0,0 +1,66 @@ +( function( $ ) { + if ( ! window.cf7_extras ) { + return; + } + + var trackGaEvent = function( eventCategory, eventAction, eventTitle ) { + if ( "function" === typeof ga ) { + ga( "send", "event", eventCategory, eventAction, eventTitle ); + } else if ( "undefined" !== typeof _gaq ) { + _gaq.push( [ "_trackEvent", eventCategory, eventAction, eventTitle ] ); + } + }; + + var formEventEnabled = function( formId, eventName ) { + formId = parseInt( formId ); + + if ( ! formId || ! window.cf7_extras.events[ eventName ] ) { + return false; + } + + if ( -1 !== window.cf7_extras.events[ eventName ].indexOf( formId ) ) { + return true; + } + + return false; + }; + + var getFormConfig = function( formId ) { + formId = parseInt( formId ); + + if ( window.cf7_extras.forms && window.cf7_extras.forms[ formId ] ) { + return window.cf7_extras.forms[ formId ]; + } + + return false; + } + + $( document ).on( 'wpcf7:mailsent', function( event, form ) { + if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga-success' ) ) { + var formConfig = getFormConfig( form.contactFormId ); + trackGaEvent( 'Contact Form', 'Sent', formConfig.title ); + } + } ); + + $( document ).on( 'wpcf7:mailfailed', function( event, form ) { + if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga-error' ) ) { + var formConfig = getFormConfig( form.contactFormId ); + trackGaEvent( 'Contact Form', 'Error', formConfig.title ); + } + } ); + + $( document ).on( 'wpcf7:submit', function( event, form ) { + if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga-submit' ) ) { + var formConfig = getFormConfig( form.contactFormId ); + trackGaEvent( 'Contact Form', 'Submit', formConfig.title ); + } + + if ( form.contactFormId && formEventEnabled( form.contactFormId, 'redirect-success' ) ) { + var formConfig = getFormConfig( form.contactFormId ); + + if ( formConfig.redirect_url ) { + window.location = formConfig.redirect_url; + } + } + } ); +} )( jQuery ); diff --git a/plugin.php b/plugin.php index 3a34b1f..35699a7 100644 --- a/plugin.php +++ b/plugin.php @@ -57,7 +57,7 @@ private function __construct() { add_action( 'wp_print_footer_scripts', array( $this, 'maybe_alter_scripts' ), 8 ); // Maybe redirect or trigger GA events - add_filter( 'wpcf7_ajax_json_echo', array( $this, 'filter_ajax_echo' ), 10, 2 ); + add_action( 'wp_print_footer_scripts', array( $this, 'track_form_events' ), 9 ); // Redirect to a custom URL really late add_action( 'wpcf7_submit', array( $this, 'wpcf7_submit' ), 987, 2 ); @@ -496,57 +496,59 @@ function dequeue_styles() { } - function filter_ajax_echo( $items, $result ) { - - $form = WPCF7_ContactForm::get_current(); - $track_ga_submit = $this->get_form_settings( $form, 'track-ga-submit' ); - - if ( ! empty( $track_ga_submit ) ) { - - if ( ! isset( $items['onSubmit'] ) ) - $items['onSubmit'] = array(); - - $items['onSubmit'][] = sprintf( - 'if ( typeof ga == "function" ) { - ga( "send", "event", "Contact Form", "Submit", "%1$s" ); - } - if ( typeof _gaq !== "undefined" ) { - _gaq.push([ "_trackEvent", "Contact Form", "Submit", "%1$s" ]); - }', - esc_js( $form->title() ) - ); + function track_form_events() { + if ( empty( $this->rendered ) ) { + return; } - if ( 'mail_sent' === $result['status'] ) { + $form_events = array( + 'track-ga-submit' => array(), + 'track-ga-success' => array(), + 'track-ga-error' => array(), + 'redirect-success' => array(), + ); - $track_ga_success = $this->get_form_settings( $form, 'track-ga-success' ); - $redirect = trim( $this->get_form_settings( $form, 'redirect-success' ) ); + $form_config = array(); - if ( ! isset( $items['onSentOk'] ) ) { - $items['onSentOk'] = array(); + foreach ( $this->rendered as $form_id => $settings ) { + + // Bail out since CF7 JS is disabled. + if ( ! empty( $settings['disable-ajax'] ) ) { + return; } - $items['onSentOk'][] = sprintf( - 'if ( typeof ga == "function" ) { - ga( "send", "event", "Contact Form", "Sent", "%1$s" ); - } - if ( typeof _gaq !== "undefined" ) { - _gaq.push([ "_trackEvent", "Contact Form", "Sent", "%1$s" ]); - }', - esc_js( $form->title() ) - ); + $form = wpcf7_contact_form( $form_id ); - if ( ! empty( $redirect ) ) { - $items['onSentOk'][] = sprintf( - 'window.location = "%s";', - esc_js( esc_url_raw( $redirect ) ) - ); + $form_config[ $form_id ] = array( + 'title' => $form->title(), + 'redirect_url' => $settings['redirect-success'], + ); + + foreach ( $form_events as $event_key => $event_form_ids ) { + if ( ! empty( $settings[ $event_key ] ) ) { + $form_events[ $event_key ][] = intval( $form_id ); + } } } - return $items; + wp_enqueue_script( + 'cf7-extras', + plugins_url( 'js/controls.js', __FILE__ ), + array( 'contact-form-7' ), + '0.0.1', + true + ); + + wp_localize_script( + 'cf7-extras', + 'cf7_extras', + array( + 'events' => $form_events, + 'forms' => $form_config, + ) + ); } From ff314022dea361967e0fec0fa50e189de858968d Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Sun, 17 Sep 2017 00:22:28 +0300 Subject: [PATCH 02/10] Combine all tracking toggles into one --- plugin.php | 45 ++++++++++++++++----------------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/plugin.php b/plugin.php index 35699a7..9a0d089 100644 --- a/plugin.php +++ b/plugin.php @@ -105,7 +105,7 @@ function wpcf7_metabox( $cf7 ) { 'docs_url' => 'http://contactform7.com/controlling-behavior-by-setting-constants/', 'field' => sprintf( '

%s

', @@ -196,37 +196,20 @@ function wpcf7_metabox( $cf7 ) { esc_html__( 'Specify the language code of the Google Recaptcha output.', 'cf7-extras' ) ) ), - 'extra-track-ga-success' => array( + 'extra-track-ga' => array( 'label' => __( 'Google Analytics Tracking', 'cf7-extras' ), 'docs_url' => 'http://contactform7.com/tracking-form-submissions-with-google-analytics/', 'field' => sprintf( - '', - checked( $settings[ 'track-ga-success' ], true, false ), - esc_html__( 'Trigger Google Analytics event on successful form submissions.', 'cf7-extras' ), + ' +

%s

', + checked( $settings[ 'track-ga' ], true, false ), + esc_html__( 'Trigger Google Analytics events on form submissions.', 'cf7-extras' ), esc_html( sprintf( __( 'Track Google Analytics event with category "Contact Form", action "Sent" and "%s" as label.', 'cf7-extras' ), $cf7->title() - ) ), - checked( $settings[ 'track-ga-submit' ], true, false ), - esc_html__( 'Trigger Google Analytics event on all form submissions.', 'cf7-extras' ), - esc_html( sprintf( - __( 'Track Google Analytics event with category "Contact Form", action "Submit" and "%s" as label.', 'cf7-extras' ), - $cf7->title() ) ) ) ) @@ -420,6 +403,7 @@ function get_form_settings( $form, $field = null, $fresh = false ) { 'redirect-success' => false, 'track-ga-success' => false, 'track-ga-submit' => false, + 'track-ga' => false, 'google-recaptcha-lang' => null, ) ); @@ -427,6 +411,11 @@ function get_form_settings( $form, $field = null, $fresh = false ) { // Cache it for re-use $form_settings[ $form->id() ] = $settings; + // Convert individual legacy settings into one. + if ( ! empty( $settings['track-ga-success'] ) || ! empty( $settings['track-ga-submit'] ) ) { + $settings['track-ga'] = true; + } + // Return a specific field value if ( isset( $field ) ) { if ( isset( $settings[ $field ] ) ) @@ -503,9 +492,7 @@ function track_form_events() { } $form_events = array( - 'track-ga-submit' => array(), - 'track-ga-success' => array(), - 'track-ga-error' => array(), + 'track-ga' => array(), 'redirect-success' => array(), ); From 76d5ba7ea08e2d3781f5ac7140fc15cfda2bdd9c Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Sun, 17 Sep 2017 00:24:22 +0300 Subject: [PATCH 03/10] Single quotes instead --- js/controls.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/js/controls.js b/js/controls.js index 48e4f09..51dd04a 100644 --- a/js/controls.js +++ b/js/controls.js @@ -4,10 +4,10 @@ } var trackGaEvent = function( eventCategory, eventAction, eventTitle ) { - if ( "function" === typeof ga ) { - ga( "send", "event", eventCategory, eventAction, eventTitle ); - } else if ( "undefined" !== typeof _gaq ) { - _gaq.push( [ "_trackEvent", eventCategory, eventAction, eventTitle ] ); + if ( 'function' === typeof ga ) { + ga( 'send', 'event', eventCategory, eventAction, eventTitle ); + } else if ( 'undefined' !== typeof _gaq ) { + _gaq.push( [ '_trackEvent', eventCategory, eventAction, eventTitle ] ); } }; From 69f2e140a9fe5f3c2c8f6473fff25164d2240985 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Sun, 17 Sep 2017 00:24:36 +0300 Subject: [PATCH 04/10] Check for a single event instead --- js/controls.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/js/controls.js b/js/controls.js index 51dd04a..d3da86d 100644 --- a/js/controls.js +++ b/js/controls.js @@ -36,21 +36,21 @@ } $( document ).on( 'wpcf7:mailsent', function( event, form ) { - if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga-success' ) ) { + if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) { var formConfig = getFormConfig( form.contactFormId ); trackGaEvent( 'Contact Form', 'Sent', formConfig.title ); } } ); $( document ).on( 'wpcf7:mailfailed', function( event, form ) { - if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga-error' ) ) { + if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) { var formConfig = getFormConfig( form.contactFormId ); trackGaEvent( 'Contact Form', 'Error', formConfig.title ); } } ); $( document ).on( 'wpcf7:submit', function( event, form ) { - if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga-submit' ) ) { + if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) { var formConfig = getFormConfig( form.contactFormId ); trackGaEvent( 'Contact Form', 'Submit', formConfig.title ); } From b829ee612e3ffdf3e0959b5b1d35cd6565335b13 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Sun, 17 Sep 2017 00:44:53 +0300 Subject: [PATCH 05/10] Do not redirect during REST calls --- plugin.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugin.php b/plugin.php index 9a0d089..1429ba6 100644 --- a/plugin.php +++ b/plugin.php @@ -545,6 +545,8 @@ function wpcf7_submit( $form, $result ) { // JS is already doing the redirect if ( isset( $_POST['_wpcf7_is_ajax_call'] ) || ! isset( $result['status'] ) ) { return; + } elseif ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { + return; } // Redirect only if this is a successful non-AJAX response From e24f783ebb0d08c276b27a7415cd182e8a146fec Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Sun, 17 Sep 2017 00:56:35 +0300 Subject: [PATCH 06/10] Update the strings --- plugin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin.php b/plugin.php index 1429ba6..18e5f63 100644 --- a/plugin.php +++ b/plugin.php @@ -180,7 +180,7 @@ function wpcf7_metabox( $cf7 ) {

%s

', esc_url( $settings[ 'redirect-success' ] ), esc_attr( 'http://example.com' ), - esc_html__( 'Enter URL where users should be redirected after successful form submissions.', 'cf7-extras' ) + esc_html__( 'Enter the URL where users should be redirected after successful form submissions.', 'cf7-extras' ) ) ), 'extra-google-recaptcha-lang' => array( @@ -208,7 +208,7 @@ function wpcf7_metabox( $cf7 ) { checked( $settings[ 'track-ga' ], true, false ), esc_html__( 'Trigger Google Analytics events on form submissions.', 'cf7-extras' ), esc_html( sprintf( - __( 'Track Google Analytics event with category "Contact Form", action "Sent" and "%s" as label.', 'cf7-extras' ), + __( 'Track form submissions as events with category "Contact Form", actions "Sent", "Error" or "Submit" and label "%s".', 'cf7-extras' ), $cf7->title() ) ) ) From e454710819c9afbedff1419e4a4853f41f6907ce Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Sun, 17 Sep 2017 01:04:45 +0300 Subject: [PATCH 07/10] Redirect only on mail sent --- js/controls.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/js/controls.js b/js/controls.js index d3da86d..6897d7d 100644 --- a/js/controls.js +++ b/js/controls.js @@ -40,6 +40,14 @@ var formConfig = getFormConfig( form.contactFormId ); trackGaEvent( 'Contact Form', 'Sent', formConfig.title ); } + + if ( form.contactFormId && formEventEnabled( form.contactFormId, 'redirect-success' ) ) { + var formConfig = getFormConfig( form.contactFormId ); + + if ( formConfig.redirect_url ) { + window.location = formConfig.redirect_url; + } + } } ); $( document ).on( 'wpcf7:mailfailed', function( event, form ) { @@ -49,18 +57,17 @@ } } ); - $( document ).on( 'wpcf7:submit', function( event, form ) { + $( document ).on( 'wpcf7:spam', function( event, form ) { if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) { var formConfig = getFormConfig( form.contactFormId ); - trackGaEvent( 'Contact Form', 'Submit', formConfig.title ); + trackGaEvent( 'Contact Form', 'Spam', formConfig.title ); } + } ); - if ( form.contactFormId && formEventEnabled( form.contactFormId, 'redirect-success' ) ) { + $( document ).on( 'wpcf7:submit', function( event, form ) { + if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) { var formConfig = getFormConfig( form.contactFormId ); - - if ( formConfig.redirect_url ) { - window.location = formConfig.redirect_url; - } + trackGaEvent( 'Contact Form', 'Submit', formConfig.title ); } } ); } )( jQuery ); From 4233d5bd13036439aef1b494e52c528b18c0f5d3 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Sun, 17 Sep 2017 01:07:22 +0300 Subject: [PATCH 08/10] Redirect very late, though --- js/controls.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/js/controls.js b/js/controls.js index 6897d7d..90dde40 100644 --- a/js/controls.js +++ b/js/controls.js @@ -40,14 +40,6 @@ var formConfig = getFormConfig( form.contactFormId ); trackGaEvent( 'Contact Form', 'Sent', formConfig.title ); } - - if ( form.contactFormId && formEventEnabled( form.contactFormId, 'redirect-success' ) ) { - var formConfig = getFormConfig( form.contactFormId ); - - if ( formConfig.redirect_url ) { - window.location = formConfig.redirect_url; - } - } } ); $( document ).on( 'wpcf7:mailfailed', function( event, form ) { @@ -69,5 +61,13 @@ var formConfig = getFormConfig( form.contactFormId ); trackGaEvent( 'Contact Form', 'Submit', formConfig.title ); } + + if ( form.contactFormId && 'mail_sent' === form.status && formEventEnabled( form.contactFormId, 'redirect-success' ) ) { + var formConfig = getFormConfig( form.contactFormId ); + + if ( formConfig.redirect_url ) { + window.location = formConfig.redirect_url; + } + } } ); } )( jQuery ); From e2ddbb3106a9839159bb4b5fbe302c6d971975f0 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Sun, 17 Sep 2017 21:57:50 +0300 Subject: [PATCH 09/10] Bump version --- plugin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin.php b/plugin.php index 18e5f63..4267075 100644 --- a/plugin.php +++ b/plugin.php @@ -6,8 +6,8 @@ Plugin URI: https://github.com/kasparsd/contact-form-7-extras Author: Kaspars Dambis Author URI: https://kaspars.net - Version: 0.3.5 - Tested up to: 4.7.3 + Version: 0.4.0 + Tested up to: 4.8.1 License: GPL2 Text Domain: cf7-extras */ From b472d47f1569df6aa71d592062860996cd830ac2 Mon Sep 17 00:00:00 2001 From: Kaspars Dambis Date: Sun, 17 Sep 2017 21:58:04 +0300 Subject: [PATCH 10/10] Bump version, add changelog --- readme.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index f20eb01..6abcd70 100644 --- a/readme.md +++ b/readme.md @@ -3,10 +3,10 @@ Contributors: kasparsd, buzztone Tags: Contact Form 7, cf7, admin, backend, redirect, tracking, google analytics, ga, simple, interface, dashboard, recaptcha Requires at least: 3.0 -Tested up to: 4.7.3 -Stable tag: trunk +Tested up to: 4.8.1 +Stable tag: 0.4.0 -Adds simple controls for some of the advanced functionality of the Contact Form 7 plugin. +Simple controls for some of the advanced Contact Form 7 plugin functionality. ## Description @@ -17,7 +17,7 @@ This plugin enables simple controls for some of the advanced features of the [Co - Disable default form CSS - Disable automatic paragraph formatting - Disable HTML5 input field types or enable the HTML5 input type fallback -- Track form submissions and completions with Google Analytics +- Track form submissions, errors and completions with Google Analytics - Redirect to URL on form submission - Specify the Google Recaptcha language @@ -49,6 +49,11 @@ The "[Storage for Contact Form 7](https://codecanyon.net/item/storage-for-contac ## Changelog +### 0.4.0 (September 17, 2017) + +- Fix Google Analytics tracking and redirect logic. +- Confirm that the plugin works with WordPress 4.8.1. + ### 0.3.5 (April 6, 2017) - Confirm that the plugin works with WordPress 4.7.3.