Skip to content

Commit

Permalink
Add tracking for the contact form block (#13688)
Browse files Browse the repository at this point in the history
* Add tracking for the contact form

* Add file

* Make it possible to track contact form submissions only coming from Gutenberg blocks

* update event name

* escape data sent to tracks

* add a jetpack prefix to the tracks event function

* add new file to the phpcs whitelist

* fix undefined index error

* fix undefined index error

* dont send tracks events in development mode

* use a different tracks implementation on wpcom

* move wpcom tracking to a different file

* Simplify approach to one single function

- keep that function in a file that is already synchronized with WordPress.com
- include both wpcom and Jetpack tracking approaches in that function.
- remove the dev mode check since this will be addressed in #13711

* Send blog ID in recorded event when available

* Feedback ID can include letters as well. Let's not sanitize as int

* Specify a user when submission is anonymous

* Use a different prefix for WordPress.com and Jetpack

If we don't do that we can't separate the 2 when looking at the data later on.

* Add missing comment *

* Switch to using record_user_event

See https://github.com/Automattic/jetpack/pull/13688/files#r339803539


Co-authored-by: Jeremy Herve <jeremy@jeremy.hu>
  • Loading branch information
scruffian and jeherve committed Oct 29, 2019
1 parent eef82a5 commit c2dd31d
Showing 1 changed file with 74 additions and 12 deletions.
86 changes: 74 additions & 12 deletions modules/contact-form/grunion-contact-form.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
<?php
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* Grunion Contact Form
* Add a contact form to any post, page or text widget.
* Emails will be sent to the post's author by default, or any email address you choose.
*
* @package Jetpack
*/

use Automattic\Jetpack\Assets;

/*
Plugin Name: Grunion Contact Form
Description: Add a contact form to any post, page or text widget. Emails will be sent to the post's author by default, or any email address you choose. As seen on WordPress.com.
Plugin URI: https://automattic.com/#
AUthor: Automattic, Inc.
Author URI: https://automattic.com/
Version: 2.4
License: GPLv2 or later
*/

use Automattic\Jetpack\Sync\Settings;

define( 'GRUNION_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
Expand Down Expand Up @@ -2083,6 +2079,10 @@ static function parse( $attributes, $content ) {
if ( is_user_logged_in() ) {
$r .= "\t\t" . wp_nonce_field( 'contact-form_' . $id, '_wpnonce', true, false ) . "\n"; // nonce and referer
}

if ( isset( $attributes['hasFormSettingsSet'] ) && $attributes['hasFormSettingsSet'] ) {
$r .= "\t\t<input type='hidden' name='is_block' value='1' />\n";
}
$r .= "\t\t<input type='hidden' name='contact-form-id' value='$id' />\n";
$r .= "\t\t<input type='hidden' name='action' value='grunion-contact-form' />\n";
$r .= "\t\t<input type='hidden' name='contact-form-hash' value='" . esc_attr( $form->hash ) . "' />\n";
Expand Down Expand Up @@ -2553,6 +2553,10 @@ function process_submission() {
$i++; // Increment prefix counter for the next extra field
}

if ( isset( $_REQUEST['is_block'] ) && $_REQUEST['is_block'] ) {
$extra_values['is_block'] = true;
}

$contact_form_subject = trim( $contact_form_subject );

$comment_author_IP = Grunion_Contact_Form_Plugin::get_ip_address();
Expand Down Expand Up @@ -3539,3 +3543,61 @@ function grunion_delete_old_spam() {
wp_schedule_single_event( time() + 700, 'grunion_scheduled_delete' );
}
}

/**
* Send an event to Tracks on form submission.
*
* @param int $post_id - the post_id for the CPT that is created.
* @param array $all_values - fields from the default contact form.
* @param array $extra_values - extra fields added to from the contact form.
*
* @return null|void
*/
function jetpack_tracks_record_grunion_pre_message_sent( $post_id, $all_values, $extra_values ) {
// Do not do anything if the submission is not from a block.
if (
! isset( $extra_values['is_block'] )
|| ! $extra_values['is_block']
) {
return;
}

/*
* Event details.
*/
$event_user = wp_get_current_user();
$event_name = 'contact_form_block_message_sent';
$event_props = array(
'entry_permalink' => esc_url( $all_values['entry_permalink'] ),
'feedback_id' => esc_attr( $all_values['feedback_id'] ),
);

/*
* Record event.
* We use different libs on wpcom and Jetpack.
*/
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
$event_name = 'wpcom_' . $event_name;
$event_props['blog_id'] = get_current_blog_id();
// If the form was sent by a logged out visitor, record event with blog owner.
if ( empty( $event_user->ID ) ) {
$event_user_id = wpcom_get_blog_owner( $event_props['blog_id'] );
$event_user = get_userdata( $event_user_id );
}

require_lib( 'tracks/client' );
tracks_record_event( $event_user, $event_name, $event_props );
} else {
// If the form was sent by a logged out visitor, record event with Jetpack master user.
if ( empty( $event_user->ID ) ) {
$master_user_id = Jetpack_Options::get_option( 'master_user' );
if ( ! empty( $master_user_id ) ) {
$event_user = get_userdata( $master_user_id );
}
}

$tracking = new Automattic\Jetpack\Tracking();
$tracking->record_user_event( $event_name, $event_props, $event_user );
}
}
add_action( 'grunion_pre_message_sent', 'jetpack_tracks_record_grunion_pre_message_sent', 12, 3 );

0 comments on commit c2dd31d

Please sign in to comment.