Skip to content

Commit

Permalink
Merge branch 'master' of github.com:kasparsd/contact-form-7-extras
Browse files Browse the repository at this point in the history
  • Loading branch information
kasparsd committed Sep 17, 2017
2 parents c74e4ab + 9769292 commit c3e7d22
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 74 deletions.
73 changes: 73 additions & 0 deletions js/controls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
( 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' ) ) {
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' ) ) {
var formConfig = getFormConfig( form.contactFormId );
trackGaEvent( 'Contact Form', 'Error', formConfig.title );
}
} );

$( document ).on( 'wpcf7:spam', function( event, form ) {
if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) {
var formConfig = getFormConfig( form.contactFormId );
trackGaEvent( 'Contact Form', 'Spam', formConfig.title );
}
} );

$( document ).on( 'wpcf7:submit', function( event, form ) {
if ( form.contactFormId && formEventEnabled( form.contactFormId, 'track-ga' ) ) {
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 );
131 changes: 61 additions & 70 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -105,7 +105,7 @@ function wpcf7_metabox( $cf7 ) {
'docs_url' => 'http://contactform7.com/controlling-behavior-by-setting-constants/',
'field' => sprintf(
'<label>
<input id="extra-disable-ajax" data-toggle-on=".extra-field-extra-track-ga-success, #extra-html5-fallback-wrap" name="extra[disable-ajax]" value="1" %s type="checkbox" />
<input id="extra-disable-ajax" data-toggle-on=".extra-field-extra-track-ga, #extra-html5-fallback-wrap" name="extra[disable-ajax]" value="1" %s type="checkbox" />
<span>%s</span>
</label>
<p class="desc">%s</p>',
Expand Down Expand Up @@ -180,7 +180,7 @@ function wpcf7_metabox( $cf7 ) {
<p class="desc">%s</p>',
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(
Expand All @@ -196,36 +196,19 @@ 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(
'<ul>
<li>
<label>
<input type="checkbox" id="extra-track-ga-success" name="extra[track-ga-success]" value="1" %s />
<span>%s</span>
</label>
<p class="desc">%s</p>
</li>
<li>
<label>
<input type="checkbox" id="extra-track-ga-submit" name="extra[track-ga-submit]" value="1" %s />
<span>%s</span>
</label>
<p class="desc">%s</p>
</li>
</ul>',
checked( $settings[ 'track-ga-success' ], true, false ),
esc_html__( 'Trigger Google Analytics event on successful 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' ),
'<label>
<input type="checkbox" id="extra-track-ga" name="extra[track-ga]" value="1" %s />
<span>%s</span>
</label>
<p class="desc">%s</p>',
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 "Submit" 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()
) )
)
Expand Down Expand Up @@ -420,13 +403,19 @@ 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,
)
);

// 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 ] ) )
Expand Down Expand Up @@ -496,57 +485,57 @@ 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' => 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();

foreach ( $this->rendered as $form_id => $settings ) {

if ( ! isset( $items['onSentOk'] ) ) {
$items['onSentOk'] = array();
// 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,
)
);

}

Expand All @@ -556,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
Expand Down
13 changes: 9 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit c3e7d22

Please sign in to comment.