-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgravity-gaparse.php
More file actions
329 lines (284 loc) · 9.12 KB
/
gravity-gaparse.php
File metadata and controls
329 lines (284 loc) · 9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
* Extends Gravity Forms to get the Google Analytics data for form submissions
* @package WordPress
*
* Plugin Name: Gravity Forms - GA Parse Add-On
* Plugin URI: http://example.com/
* Description: Grab the Google Analytics data for form submissions including campaign source, campaign name, campaign medium among other data
* Version: 0.1
* Author: De"Yonte W.
* Author URI: http://example.com/
*/
/**
* Copyright (c) 2013 De"Yonte W. All rights reserved.
*
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
*/
// File Security Check
if ( ! empty( $_SERVER['SCRIPT_FILENAME'] ) && basename( __FILE__ ) == basename( $_SERVER['SCRIPT_FILENAME'] ) ) {
die ( 'You do not have sufficient permissions to access this page!' );
}
if( !class_exists('GA_Parse') )
require("admin/class.gaparse.php");
/**
* Class to provide Google Analytics parsing for Gravity Forms
* @subpackage Gravity Forms
* @category Plugin
* @author De'Yonte W.
*/
class GravityGAParse extends GA_Parse{
public $ga_fields;
/**
* create the constructor for the class
*
* create the constructor for the child class and call the parent constructor
*/
public function __construct(){
parent::__construct($_COOKIE);
$this->ga_fields = array(
array(
'id' => 'ga_campaign_source',
'name' => 'Campaign Source'
),
array(
'id' => 'ga_campaign_name',
'name' => 'Campaign Name'
),
array(
'id' => 'ga_campaign_medium',
'name' => 'Campaign Medium'
),
array(
'id' => 'ga_campaign_content',
'name' => 'Campaign Content'
),
array(
'id' => 'ga_campaign_term',
'name' => 'Campaign Term'
),
array(
'id' => 'ga_first_visit',
'name' => 'First Visit'
),
array(
'id' => 'ga_previous_visit',
'name' => 'Previous Visit'
),
array(
'id' => 'ga_visit_started',
'name' => 'Current Visit Started'
),
array(
'id' => 'ga_times_visited',
'name' => 'Times Visited'
),
array(
'id' => 'ga_pages_viewed',
'name' => 'Pages Viewed'
)
);
add_action( "gform_field_input" , array($this,"render_field"), 10, 5 );
if( is_admin() ):
$this->admin_hooks();
else:
$this->front_hooks();
endif;
}
/*================START ADMIN SECTION================*/
/**
* load the WordPress hooks for the admin/backend side of WordPress
*/
public function admin_hooks(){
add_filter("gform_add_field_buttons", array($this,"define_fields"));
add_filter("gform_field_type_title" , array($this,"add_field_titles"));
add_action("gform_editor_js", array($this,"field_settings" ));
}
/**
* add a new field group to Gravity Forms and the custom field types
*
* add the Google Analytics field group to Gravity Forms and assign the new field types to display in the group
* @param array $field_groups
* @return array the field groups to display and the custom field types
*/
public function define_fields( $field_groups ) {
$field_groups[] = array(
"name" => "google_analytics",
"label" => "Google Analytics",
"tooltip_class" => "tooltip_bottomleft",
"fields" => array()
);
//render all of the google analytic fields
$all_fields = array();
foreach( $this->ga_fields as $ga_field ):
$all_fields[] = array(
"class" => "button",
"value" => $ga_field["name"],
"onclick" => "StartAddField('".$ga_field["id"]."')",
);
endforeach;
$field_groups[count($field_groups)-1]["fields"] = $all_fields;
return $field_groups;
}
/**
* add field titles for the custom field types
*
* add the field titles for each of the custom field types. This appears when you hover over the field on the backend of the Gravity Forms editor page
* @param string $type
* @return string the title of the field
*/
public function add_field_titles( $type ) {
$title = null;
switch($type){
case "ga_campaign_source": $title = "Campaign Source";
break;
case "ga_campaign_name": $title = "Campaign Name";
break;
case "ga_campaign_medium": $title = "Campaign Medium";
break;
case "ga_campaign_content": $title = "Campaign Content";
break;
case "ga_campaign_term": $title = "Campaign Term";
break;
case "ga_first_visit": $title = "First Visit";
break;
case "ga_previous_visit": $title = "Previous Visit";
break;
case "ga_visit_started": $title = "Current Visit Started";
break;
case "ga_times_visited": $title = "Times Visited";
break;
case "ga_pages_viewed": $title = "Pages Viewed";
break;
}
if( !is_null($title) ){
return __( $title , "gravityforms" );
}
}
/**
* load the field on the backend and the frontend of the site
* @param string $input
* @param object $field
* @param string $value
* @param int $lead_id
* @param int $form_id
* @return string how the field should be displayed
*/
public function render_field( $input, $field, $value, $lead_id, $form_id ){
$type = $field["type"];
$tabindex = GFCommon::get_tabindex();
$input_name = $form_id ."_" . $field["id"];
$ga_field = null;
if( $this->in_array_r($type, $this->ga_fields) )
return sprintf( "<div class='ginput_container'><input type='hidden' name='input_%s' id='%s' value='%s'></div>", $field["id"], $type."-".$field["id"], $type );
return $input;
}
/**
* assign field settings for the custom field type
*
* Gravity Forms Field settings for the custom field type including label, desrciption, etc...
*/
public function field_settings(){
?>
<script type='text/javascript'>
jQuery(document).ready(function($) {
<?php foreach($this->ga_fields as $field_setting ):?>
fieldSettings['<?php echo $field_setting["id"];?>'] = ".label_setting";
<?php endforeach;?>
});
</script>
<?php
}
/**
* assign a CSS class to the parent <li> element
* @param string $classes
* @param array $field
* @param object $form
* @return string class or classes for the element
*/
public function css_class($classes, $field, $form){
$type = $field['type'];
if( $this->in_array_r($type, $this->ga_fields) )
$classes .= ' gform_hidden';
return $classes;
}
/**
* recursive in_array function
*
* recursively search multidimensional arrays
* @link http://stackoverflow.com/questions/4128323/in-array-and-multidimensional-array
* @return boolean if element is found or not found
*/
public function in_array_r($needle, $haystack, $strict = false){
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && $this->in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
/*================END ADMIN SECTION================*/
/**
* load the WordPress hooks for the front side of WordPress
*/
public function front_hooks(){
add_action("gform_field_css_class", array($this,"css_class"), 10, 3);
add_action("gform_pre_submission", array($this,"get_values"));
add_action("gform_enqueue_scripts", array($this,'load_css'), 10, 2);
}
public function load_css(){?>
<style type="text/css">
.gform_hidden{
display: none!important;
}
</style>
<?php
}
/**
* assign the Google Analytic values to the custom fields
*
* grab the Google Analytics data for the form submission. assign the values to the appropriate fields
* @param object $form
*/
public function get_values($form){
foreach( $_POST as $key=>$field ):
if( $field == 'ga_campaign_source' )
$_POST[$key] = (isset($_GET['utm_source'])?$_GET['utm_source']:$this->campaign_source);
elseif( $field == 'ga_campaign_name' )
$_POST[$key] = (isset($_GET['utm_campaign'])?$_GET['utm_campaign']:$this->campaign_name);
elseif( $field == 'ga_campaign_medium' )
$_POST[$key] = (isset($_GET['utm_medium'])?$_GET['utm_medium']:$this->campaign_medium);
elseif( $field == 'ga_campaign_content' )
$_POST[$key] = (isset($_GET['utm_content'])?$_GET['utm_content']:$this->campaign_content);
elseif( $field == 'ga_campaign_term' )
$_POST[$key] = $this->campaign_term;
elseif( $field == 'ga_first_visit' )
$_POST[$key] = $this->first_visit;
elseif( $field == 'ga_previous_visit' )
$_POST[$key] = $this->previous_visit;
elseif( $field == 'ga_visit_started' )
$_POST[$key] = $this->current_visit_started;
elseif( $field == 'ga_times_visited' )
$_POST[$key] = $this->times_visited;
elseif( $field == 'ga_pages_viewed' )
$_POST[$key] = $this->pages_viewed;
endforeach;
}
}
$gravity_gaparse = new GravityGAParse();