Event Tracking for Gravity Forms - Version 1.6.0

Version Description

  • Refactored the plugin to use feeds. Now you can have multiple feeds with conditions!
Download this release

Release Info

Developer nmarks
Plugin Icon 128x128 Event Tracking for Gravity Forms
Version 1.6.0
Comparing to
See all releases

Code changes from version 1.5.5 to 1.6.0

README.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: nmarks
3
  Tags: gravity forms, google analytics, event tracking
4
  Requires at least: 3.5.2
5
  Tested up to: 4.1
6
- Stable tag: 1.5.5
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -19,9 +19,11 @@ Looking for help setting things up? [Read My Setup Guide](http://nvis.io/x8fld)
19
  = Minimum Requirements =
20
  - PHP 5.3+
21
  - Gravity Forms 1.8.20+
 
22
 
23
  = Features =
24
  - Automatically send form submission events to Google Analytics
 
25
  - Custom event categories, actions, labels and even values
26
  - Dynamic event value on payment forms (integration with the payment add-ons including Paypal Standard, PayPal Pro, Stripe, etc...)
27
  - Awesomeness
@@ -76,10 +78,14 @@ Check out the documentation on [github](https://github.com/nathanmarks/wordpress
76
  == Screenshots ==
77
 
78
  1. The Gravity Forms setting screen where you setup your UA ID.
79
- 2. The form settings where you set your category, action and label.
 
80
 
81
  == Changelog ==
82
 
 
 
 
83
  = 1.5.5 =
84
  * Hotfix for issue with paypal standard converting early
85
 
3
  Tags: gravity forms, google analytics, event tracking
4
  Requires at least: 3.5.2
5
  Tested up to: 4.1
6
+ Stable tag: 1.6.0
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
19
  = Minimum Requirements =
20
  - PHP 5.3+
21
  - Gravity Forms 1.8.20+
22
+ - Google Analytics Universal Analytics (classic is not supported by Google's new fancy stuff this plugin leverages)
23
 
24
  = Features =
25
  - Automatically send form submission events to Google Analytics
26
+ - Add multiple event feeds with conditionals
27
  - Custom event categories, actions, labels and even values
28
  - Dynamic event value on payment forms (integration with the payment add-ons including Paypal Standard, PayPal Pro, Stripe, etc...)
29
  - Awesomeness
78
  == Screenshots ==
79
 
80
  1. The Gravity Forms setting screen where you setup your UA ID.
81
+ 2. The form settings feed list.
82
+ 3. The feed settings page
83
 
84
  == Changelog ==
85
 
86
+ = 1.6.0 =
87
+ * Refactored the plugin to use feeds. Now you can have multiple feeds with conditions!
88
+
89
  = 1.5.5 =
90
  * Hotfix for issue with paypal standard converting early
91
 
gravity-forms-event-tracking.php CHANGED
@@ -10,7 +10,7 @@
10
  * Plugin Name: Gravity Forms Google Analytics Event Tracking
11
  * Plugin URI: https://wordpress.org/plugins/gravity-forms-google-analytics-event-tracking/
12
  * Description: Add Google Analytics event tracking to your Gravity Forms with ease.
13
- * Version: 1.5.5
14
  * Author: Nathan Marks
15
  * Author URI: http://www.nvisionsolutions.ca
16
  * Text Domain: gravity-forms-google-analytics-event-tracking
@@ -29,11 +29,11 @@ class Gravity_Forms_Event_Tracking_Bootstrap {
29
 
30
  public static function load(){
31
 
32
- if ( ! method_exists( 'GFForms', 'include_addon_framework' ) ) {
33
  return;
34
  }
35
 
36
- require_once( plugin_dir_path( __FILE__ ) . 'includes/class-gravity-forms-event-tracking.php' );
37
 
38
  GFAddOn::register( 'Gravity_Forms_Event_Tracking' );
39
 
10
  * Plugin Name: Gravity Forms Google Analytics Event Tracking
11
  * Plugin URI: https://wordpress.org/plugins/gravity-forms-google-analytics-event-tracking/
12
  * Description: Add Google Analytics event tracking to your Gravity Forms with ease.
13
+ * Version: 1.6.0
14
  * Author: Nathan Marks
15
  * Author URI: http://www.nvisionsolutions.ca
16
  * Text Domain: gravity-forms-google-analytics-event-tracking
29
 
30
  public static function load(){
31
 
32
+ if ( ! method_exists( 'GFForms', 'include_feed_addon_framework' ) ) {
33
  return;
34
  }
35
 
36
+ require_once( plugin_dir_path( __FILE__ ) . 'includes/class-gravity-forms-event-tracking-feed.php' );
37
 
38
  GFAddOn::register( 'Gravity_Forms_Event_Tracking' );
39
 
includes/class-gravity-forms-event-tracking-feed.php ADDED
@@ -0,0 +1,575 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Gravity forms event tracking
4
+ *
5
+ * @package Gravity_Forms_Event_Tracking
6
+ * @author Nathan Marks <nmarks@nvisionsolutions.ca>
7
+ * @license GPL-2.0+
8
+ * @link http://www.nvisionsolutions.ca
9
+ * @copyright 2014 Nathan Marks
10
+ */
11
+
12
+
13
+ GFForms::include_feed_addon_framework();
14
+
15
+ class Gravity_Forms_Event_Tracking extends GFFeedAddOn {
16
+
17
+ protected $_version = "1.6.0";
18
+ protected $_min_gravityforms_version = "1.8.20";
19
+
20
+ /**
21
+ * The actual slug of this plugin is too long for GF settings to save properly.
22
+ * With the appended/prepended string it attempts to insert an option over
23
+ * 64 chars in length.
24
+ *
25
+ * @TODO Resolve this in 2.0 somehow...
26
+ */
27
+ protected $_slug = "gravity-forms-event-tracking";
28
+ protected $_text_domain = "gravity-forms-google-analytics-event-tracking";
29
+ protected $_path = "gravity-forms-google-analytics-event-tracking/gravity-forms-event-tracking.php";
30
+ protected $_full_path = __FILE__;
31
+ protected $_url = "https://wordpress.org/plugins/gravity-forms-google-analytics-event-tracking";
32
+ protected $_title = "Gravity Forms Google Analytics Event Tracking";
33
+ protected $_short_title = "Event Tracking";
34
+
35
+ public $ua_id = false;
36
+
37
+ private static $_instance = null;
38
+
39
+ public static function get_instance() {
40
+ if ( self::$_instance == null ) {
41
+ self::$_instance = new Gravity_Forms_Event_Tracking();
42
+ }
43
+
44
+ return self::$_instance;
45
+ }
46
+
47
+ /**
48
+ * Overriding init function to change the load_plugin_textdomain call.
49
+ * See comment above for explanation.
50
+ */
51
+ public function init() {
52
+
53
+ load_plugin_textdomain( $this->_text_domain, false, $this->_text_domain . '/languages' );
54
+
55
+ add_filter( 'gform_logging_supported', array( $this, 'set_logging_supported' ) );
56
+ add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_action_links' ) );
57
+
58
+ if ( RG_CURRENT_PAGE == 'admin-ajax.php' ) {
59
+
60
+ //If gravity forms is supported, initialize AJAX
61
+ if ( $this->is_gravityforms_supported() ) {
62
+ $this->init_ajax();
63
+ }
64
+ } else if ( is_admin() ) {
65
+
66
+ $this->init_admin();
67
+
68
+ } else {
69
+
70
+ if ( $this->is_gravityforms_supported() ) {
71
+ $this->init_frontend();
72
+ }
73
+
74
+ }
75
+
76
+ }
77
+
78
+ /**
79
+ * Public facing init
80
+ *
81
+ * @since 1.5.0
82
+ */
83
+ public function init_frontend() {
84
+
85
+ parent::init_frontend();
86
+
87
+ // Move this hook so everything else is all done and dusted first!
88
+ remove_filter( 'gform_entry_post_save', array( $this, 'maybe_process_feed' ) );
89
+
90
+
91
+ if ( $this->load_ua_settings() ) {
92
+ $this->load_measurement_client();
93
+
94
+ add_filter( 'gform_after_submission', array( $this, 'maybe_process_feed' ), 10, 2 );
95
+
96
+ // IPN hook for paypal standard!
97
+ if ( class_exists( 'GFPayPal' ) ) {
98
+ add_action( 'gform_paypal_post_ipn', array( $this, 'paypal_track_form_post_ipn' ), 10, 2 );
99
+ }
100
+ }
101
+
102
+ }
103
+
104
+ /**
105
+ * Process the feed!
106
+ * @param array $feed feed data and settings
107
+ * @param array $entry gf entry object
108
+ * @param array $form gf form data
109
+ */
110
+ public function process_feed( $feed, $entry, $form ) {
111
+
112
+ $paypal_feeds = $this->get_feeds_by_slug( 'gravityformspaypal', $form['id'] );
113
+ $has_paypal_feed = false;
114
+
115
+ foreach ( $paypal_feeds as $paypal_feed ){
116
+ if ( $paypal_feed['is_active'] && $this->is_feed_condition_met( $paypal_feed, $form, $entry ) ){
117
+ $has_paypal_feed = true;
118
+ break;
119
+ }
120
+ }
121
+
122
+ $ga_event_data = $this->get_event_data( $feed, $entry, $form );
123
+
124
+ if ( $has_paypal_feed ) {
125
+ gform_update_meta( $entry['id'], 'ga_event_data', maybe_serialize( $ga_event_data ) );
126
+ }
127
+ else {
128
+ $this->track_form_after_submission( $entry, $form, $ga_event_data );
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Load UA Settings
134
+ *
135
+ * @since 1.4.0
136
+ * @return bool Returns true if UA ID is loaded, false otherwise
137
+ */
138
+ private function load_ua_settings() {
139
+ $gravity_forms_add_on_settings = get_option( 'gravityformsaddon_gravity-forms-event-tracking_settings', array() );
140
+
141
+ $this->ua_id = $ua_id = false;
142
+
143
+ $ua_id = $gravity_forms_add_on_settings[ 'gravity_forms_event_tracking_ua' ];
144
+
145
+ $ua_regex = "/^UA-[0-9]{5,}-[0-9]{1,}$/";
146
+
147
+ if ( preg_match( $ua_regex, $ua_id ) ) {
148
+ $this->ua_id = $ua_id;
149
+ return true;
150
+ }
151
+
152
+ if ( ! $this->ua_id )
153
+ return false;
154
+ }
155
+
156
+ /**
157
+ * Load the google measurement protocol PHP client.
158
+ *
159
+ * @since 1.4.0
160
+ */
161
+ private function load_measurement_client() {
162
+ require_once( 'vendor/ga-mp/src/Racecore/GATracking/Autoloader.php');
163
+ Racecore\GATracking\Autoloader::register( dirname(__FILE__) . '/vendor/ga-mp/src/' );
164
+ }
165
+
166
+ /**
167
+ * Get data required for processing
168
+ * @param array $feed feed
169
+ * @param array $entry GF Entry object
170
+ * @param array $form GF Form object
171
+ */
172
+ private function get_event_data( $feed, $entry, $form ) {
173
+ global $post;
174
+
175
+ // Paypal will need this cookie for the IPN
176
+ $ga_cookie = $_COOKIE['_ga'];
177
+
178
+ // Location
179
+ $document_location = 'http' . ( isset( $_SERVER['HTTPS'] ) ? 's' : '' ) . '://' . $_SERVER['HTTP_HOST'] . '/' . $_SERVER['REQUEST_URI'];
180
+
181
+ // Title
182
+ $document_title = isset( $post ) && get_the_title( $post ) ? get_the_title( $post ) : 'no title';
183
+
184
+ // Store everything we need for later
185
+ $ga_event_data = array(
186
+ 'ga_cookie' => $_COOKIE['_ga'],
187
+ 'document_location' => $document_location,
188
+ 'document_title' => $document_title,
189
+ 'gaEventCategory' => $this->get_event_var( 'gaEventCategory', $feed, $entry, $form ),
190
+ 'gaEventAction' => $this->get_event_var( 'gaEventAction', $feed, $entry, $form ),
191
+ 'gaEventLabel' => $this->get_event_var( 'gaEventLabel', $feed, $entry, $form ),
192
+ 'gaEventValue' => $this->get_event_var( 'gaEventValue', $feed, $entry, $form ),
193
+ );
194
+
195
+ return $ga_event_data;
196
+ }
197
+
198
+ /**
199
+ * Get our event vars
200
+ */
201
+ private function get_event_var( $var, $feed, $entry, $form ) {
202
+
203
+ if ( isset( $feed['meta'][ $var ] ) && ! empty( $feed['meta'][ $var ] ) ) {
204
+ return $feed['meta'][ $var ];
205
+ }
206
+ else {
207
+ switch ( $var ) {
208
+ case 'gaEventCategory':
209
+ return 'Forms';
210
+
211
+ case 'gaEventAction':
212
+ return 'Submission';
213
+
214
+ case 'gaEventLabel':
215
+ return 'Form: {form_title} ID: {form_id}';
216
+
217
+ case 'gaEventValue':
218
+ return false;
219
+
220
+ default:
221
+ return false;
222
+ }
223
+ }
224
+
225
+ }
226
+
227
+ /**
228
+ * Handle the form after submission before sending to the event push
229
+ *
230
+ * @since 1.4.0
231
+ * @param array $entry Gravity Forms entry object
232
+ * @param array $form Gravity Forms form object
233
+ */
234
+ private function track_form_after_submission( $entry, $form, $ga_event_data ) {
235
+
236
+ // Try to get payment amount
237
+ // This needs to go here in case something changes with the amount
238
+ if ( ! $ga_event_data['gaEventValue'] ) {
239
+ $ga_event_data['gaEventValue'] = $this->get_event_value( $entry, $form );
240
+ }
241
+
242
+ $event_vars = array( 'gaEventCategory', 'gaEventAction', 'gaEventLabel', 'gaEventValue' );
243
+
244
+ foreach ( $event_vars as $var ) {
245
+ if ( $ga_event_data[ $var ] ) {
246
+ $ga_event_data[ $var ] = GFCommon::replace_variables( $ga_event_data[ $var ], $form, $entry, false, false, true, 'text' );
247
+ }
248
+ }
249
+
250
+
251
+ // Push the event to google
252
+ $this->push_event( $entry, $form, $ga_event_data );
253
+ }
254
+
255
+ /**
256
+ * Handle the IPN response for pushing the event
257
+ *
258
+ * @since 1.4.0
259
+ * @param array $post_object global post array from the IPN
260
+ * @param array $entry Gravity Forms entry object
261
+ */
262
+ public function paypal_track_form_post_ipn( $post_object, $entry ) {
263
+ // Check if the payment was completed before continuing
264
+ if ( strtolower( $entry['payment_status'] ) != 'paid' ) {
265
+ return;
266
+ }
267
+
268
+ $form = GFFormsModel::get_form_meta( $entry['form_id'] );
269
+
270
+ $ga_event_data = maybe_unserialize( gform_get_meta( $entry['id'], 'ga_event_data' ) );
271
+
272
+ // Override this coming from paypal IPN
273
+ $_COOKIE['_ga'] = $ga_event_data['ga_cookie'];
274
+
275
+ // Push the event to google
276
+ $this->push_event( $entry, $form, $ga_event_data );
277
+ }
278
+
279
+ /**
280
+ * Push the Google Analytics Event!
281
+ *
282
+ * @since 1.4.0
283
+ * @param array $event Gravity Forms event object
284
+ * @param array $form Gravity Forms form object
285
+ */
286
+ private function push_event( $entry, $form, $ga_event_data ) {
287
+
288
+ // Init tracking object
289
+ $this->tracking = new \Racecore\GATracking\GATracking( apply_filters( 'gform_ua_id', $this->ua_id, $form ), true );
290
+ $event = new \Racecore\GATracking\Tracking\Event();
291
+
292
+ // Set some defaults
293
+ $event->setDocumentLocation( $ga_event_data['document_location'] );
294
+ $event->setDocumentTitle( $ga_event_data['document_title'] );
295
+
296
+ // Set our event object variables
297
+ $event->setEventCategory( apply_filters( 'gform_event_category', $ga_event_data['gaEventCategory'], $form ) );
298
+ $event->setEventAction( apply_filters( 'gform_event_action', $ga_event_data['gaEventAction'], $form ) );
299
+ $event->setEventLabel( apply_filters( 'gform_event_label', $ga_event_data['gaEventLabel'], $form ) );
300
+
301
+
302
+ if ( $event_value = apply_filters( 'gform_event_value', $ga_event_data['gaEventValue'], $form ) ) {
303
+ // Event value must be a valid float!
304
+ $event_value = (float) $event_value;
305
+ $event->setEventValue( $event_value );
306
+ }
307
+
308
+ // Pppp Push it!
309
+ $this->tracking->addTracking( $event );
310
+
311
+ echo "woof";
312
+
313
+ try {
314
+ $this->tracking->send();
315
+ } catch (Exception $e) {
316
+ error_log( $e->getMessage() . ' in ' . get_class( $e ) );
317
+ }
318
+ }
319
+
320
+ /**
321
+ * Get the event value for payment entries
322
+ *
323
+ * @since 1.4.0
324
+ * @param array $event Gravity Forms event object
325
+ * @return string/boolean Event value or false if not a payment form
326
+ */
327
+ private function get_event_value( $entry ) {
328
+ $value = rgar( $entry, 'payment_amount' );
329
+
330
+ if ( ! empty( $value ) && intval( $value ) ) {
331
+ return intval( $value );
332
+ }
333
+
334
+ return false;
335
+ }
336
+
337
+ /**
338
+ * Plugin settings fields
339
+ *
340
+ * @return array Array of plugin settings
341
+ */
342
+ public function plugin_settings_fields() {
343
+ return array(
344
+ array(
345
+ 'title' => __( 'Google Analytics', $this->_text_domain ),
346
+ 'description' => __( 'Enter your UA code (UA-XXXX-Y) here.', $this->_text_domain ),
347
+ 'fields' => array(
348
+ array(
349
+ 'name' => 'gravity_forms_event_tracking_ua',
350
+ 'label' => __( 'UA Tracking ID', $this->_text_domain ),
351
+ 'type' => 'text',
352
+ 'class' => 'medium',
353
+ 'tooltip' => 'UA-XXXX-Y',
354
+ 'feedback_callback' => array( $this, 'ua_validation' )
355
+ ),
356
+ )
357
+ ),
358
+ );
359
+ }
360
+
361
+ //---------- Form Settings Pages --------------------------
362
+
363
+ /**
364
+ * Form settings page title
365
+ *
366
+ * @since 1.5.0
367
+ * @return string Form Settings Title
368
+ */
369
+ public function feed_settings_title() {
370
+ return __( 'Event Tracking Feed Settings', $this->_text_domain );
371
+ }
372
+
373
+ /**
374
+ * Form settings fields
375
+ *
376
+ * @since 1.5.0
377
+ * @return array Array of form settings
378
+ */
379
+ public function feed_settings_fields() {
380
+ return array(
381
+ array(
382
+ "title" => __( 'Feed Settings', $this->_text_domain ),
383
+ "fields" => array(
384
+ array(
385
+ 'label' => __( 'Feed Name', $this->_text_domain ),
386
+ 'type' => 'text',
387
+ 'name' => 'feedName',
388
+ 'class' => 'medium',
389
+ 'required' => true,
390
+ 'tooltip' => '<h6>' . __( 'Feed Name', $this->_text_domain ) . '</h6>' . __( 'Enter a feed name to uniquely identify this setup.', $this->_text_domain )
391
+ )
392
+ ),
393
+ ),
394
+ array(
395
+ "title" => __( 'Event Tracking Settings', $this->_text_domain ),
396
+ "fields" => array(
397
+ array(
398
+ "label" => "",
399
+ "type" => "instruction_field",
400
+ "name" => "instructions"
401
+ ),
402
+ array(
403
+ "label" => __( 'Event Category', $this->_text_domain ),
404
+ "type" => "text",
405
+ "name" => "gaEventCategory",
406
+ "class" => "medium merge-tag-support mt-position-right",
407
+ "tooltip" => sprintf( '<h6>%s</h6>%s', __( 'Event Category', $this->_text_domain ), __( 'Enter your Google Analytics event category', $this->_text_domain ) ),
408
+ ),
409
+ array(
410
+ "label" => __( 'Event Action', $this->_text_domain ),
411
+ "type" => "text",
412
+ "name" => "gaEventAction",
413
+ "class" => "medium merge-tag-support mt-position-right",
414
+ "tooltip" => sprintf( '<h6>%s</h6>%s', __( 'Event Action', $this->_text_domain ), __( 'Enter your Google Analytics event action', $this->_text_domain ) ),
415
+ ),
416
+ array(
417
+ "label" => __( 'Event Label', $this->_text_domain ),
418
+ "type" => "text",
419
+ "name" => "gaEventLabel",
420
+ "class" => "medium merge-tag-support mt-position-right",
421
+ "tooltip" => sprintf( '<h6>%s</h6>%s', __( 'Event Label', $this->_text_domain ), __( 'Enter your Google Analytics event label', $this->_text_domain ) ),
422
+ ),
423
+ array(
424
+ "label" => __( 'Event Value', $this->_text_domain ),
425
+ "type" => "text",
426
+ "name" => "gaEventValue",
427
+ "class" => "medium merge-tag-support mt-position-right",
428
+ "tooltip" => sprintf( '<h6>%s</h6>%s', __( 'Event Value', $this->_text_domain ), __( 'Enter your Google Analytics event value. Leave blank to omit pushing a value to Google Analytics. Or to use the purchase value of a payment based form. <strong>Note:</strong> This must be a number (int/float).', $this->_text_domain ) ),
429
+ ),
430
+ )
431
+ ),
432
+ array(
433
+ "title" => __( 'Other Settings', $this->_text_domain ),
434
+ "fields" => array(
435
+ array(
436
+ 'name' => 'conditionalLogic',
437
+ 'label' => __( 'Conditional Logic', $this->_text_domain ),
438
+ 'type' => 'feed_condition',
439
+ 'tooltip' => '<h6>' . __( 'Conditional Logic', $this->_text_domain ) . '</h6>' . __( 'When conditions are enabled, events will only be sent to google when the conditions are met. When disabled, all form submissions will trigger an event.', $this->_text_domain )
440
+ )
441
+ )
442
+ ),
443
+ );
444
+ }
445
+
446
+ /**
447
+ * Instruction field
448
+ *
449
+ * @since 1.5.0
450
+ */
451
+ public function single_setting_row_instruction_field(){
452
+ echo '
453
+ <tr>
454
+ <th colspan="2">
455
+ <p>' . __( "If you leave these blank, the following defaults will be used when the event is tracked", $this->_text_domain ) . ':</p>
456
+ <p>
457
+ <strong>' . __( "Event Category", $this->_text_domain ) . ':</strong> Forms<br>
458
+ <strong>' . __( "Event Action", $this->_text_domain ) . ':</strong> Submission<br>
459
+ <strong>' . __( "Event Label", $this->_text_domain ) . ':</strong> Form: {form_title} ID: {form_id}<br>
460
+ <strong>' . __( "Event Value", $this->_text_domain ) . ':</strong> Payment Amount (on payment forms only, otherwise nothing is sent by default)
461
+ </p>
462
+ </td>
463
+ </tr>';
464
+ }
465
+
466
+ /**
467
+ * Return the feed list columns
468
+ * @return array columns
469
+ */
470
+ public function feed_list_columns() {
471
+ return array(
472
+ 'feedName' => __( 'Name', $this->_text_domain ),
473
+ 'gaEventCategory' => __( 'Category', $this->_text_domain ),
474
+ 'gaEventAction' => __( 'Action', $this->_text_domain ),
475
+ 'gaEventLabel' => __( 'Label', $this->_text_domain ),
476
+ 'gaEventValue' => __( 'Value', $this->_text_domain ),
477
+ );
478
+ }
479
+
480
+ //-------------- Setup ---------------
481
+
482
+ /**
483
+ * Upgrading functions
484
+ *
485
+ * @since 1.5.0
486
+ */
487
+ public function upgrade( $previous_version ) {
488
+
489
+ // If the version is below 1.5.0, we need to move the form specific settings
490
+ if ( version_compare( $previous_version, "1.5.0" ) == -1 ) {
491
+ $forms = GFAPI::get_forms( true );
492
+
493
+ foreach ( $forms as $form ) {
494
+ $this->upgrade_old_form_settings( $form );
495
+ }
496
+ }
497
+
498
+ // If the version is below 1.6.0, we need to convert any form settings to a feed
499
+ if ( version_compare( $previous_version, "1.6.0" ) == -1 ) {
500
+ $forms = GFAPI::get_forms( true );
501
+
502
+ foreach ( $forms as $form ) {
503
+ $this->upgrade_settings_to_feed( $form );
504
+ }
505
+ }
506
+
507
+ }
508
+
509
+ /**
510
+ * Upgrade old settings created prior to new settings tab
511
+ *
512
+ * @since 1.5.0
513
+ * @param array $form GF Form Object Array
514
+ */
515
+ public function upgrade_old_form_settings( $form ) {
516
+ $settings = array( 'gaEventCategory', 'gaEventAction', 'gaEventLabel', 'gaEventValue' );
517
+
518
+ foreach( $settings as $key => $setting ) {
519
+ if ( isset( $form[ $setting ] ) && ( ! isset( $form[ $this->_slug ] ) || ! isset( $form[ $this->_slug ][ $setting ] ) || empty( $form[ $this->_slug ][ $setting ] ) ) ) {
520
+ $form[ $this->_slug ][ $setting ] = $form[ $setting ];
521
+ }
522
+ unset( $form[ $setting ] );
523
+ }
524
+
525
+ GFFormsModel::update_form_meta( $form['id'], $form );
526
+ }
527
+
528
+ /**
529
+ * Upgrade settings and convert them into a feed
530
+ *
531
+ * @since 1.6.0
532
+ * @param array $form GF Form object Array
533
+ */
534
+ public function upgrade_settings_to_feed( $form ) {
535
+ if ( isset( $form['gravity-forms-event-tracking'] ) && $previous_settings = $form['gravity-forms-event-tracking'] ) {
536
+
537
+ $field_names = array( 'gaEventCategory', 'gaEventAction', 'gaEventLabel', 'gaEventValue' );
538
+
539
+ $previous_was_setup = false;
540
+
541
+ $previous_was_enabled = isset( $previous_settings['gaEventTrackingDisabled'] ) ? ! $previous_settings['gaEventTrackingDisabled'] : true;
542
+
543
+ $settings = array();
544
+
545
+ foreach ($field_names as $field_name) {
546
+ if ( isset( $previous_settings[ $field_name ] ) && $previous_settings[ $field_name ] ) {
547
+ $settings[ $field_name ] = $previous_settings[ $field_name ];
548
+ $previous_was_setup = true;
549
+ }
550
+
551
+ unset( $form['gravity-forms-event-tracking'] );
552
+
553
+ GFFormsModel::update_form_meta( $form['id'], $form );
554
+ }
555
+
556
+ if ( ! $previous_was_setup && ! $previous_was_enabled ) {
557
+ return;
558
+ }
559
+
560
+ $settings['feedName'] = __( 'Event Tracking Feed', $this->_text_domain );
561
+
562
+ $feed_id = $this->save_feed_settings( 0, $form['id'], $settings );
563
+
564
+ if ( ! $previous_was_enabled ) {
565
+ $this->update_feed_active( $feed_id, false );
566
+ }
567
+
568
+ }
569
+ else {
570
+ return;
571
+ }
572
+
573
+ }
574
+
575
+ }