WP Google Analytics Events - Version 1.1

Version Description

Download this release

Release Info

Developer yuvalo
Plugin Icon 128x128 WP Google Analytics Events
Version 1.1
Comparing to
See all releases

Version 1.1

ga-scroll-event.php ADDED
@@ -0,0 +1,309 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: WP Google Analytics Events
4
+ Plugin URI: http://wpgoogleanalyticsevents.com
5
+ Description: Adds the Google Analytics code to your website and enables you to send events on scroll or click.
6
+ Version: 1.1
7
+ Author: Yuval Oren
8
+ Author URI: http://wpgoogleanalyticsevents.com
9
+ License: GPLv2
10
+ */
11
+
12
+ /* Copyright 2013 Yuval Oren (email : yuval@steam.io)
13
+ 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.
14
+ 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.
15
+ You should have received a copy of the GNU General Public License
16
+ along with this program; if not, write to the Free Software
17
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+
21
+
22
+ /*
23
+ * Plugin Activation
24
+ */
25
+
26
+ register_activation_hook( __FILE__, 'ga_events_install' );
27
+
28
+
29
+ function ga_events_install() {
30
+ $ga_events_options = array(
31
+ 'id' => '',
32
+ 'divs' => array(array(id => '',type =>'', action => '', cat => '', label => '')),
33
+ 'click' => array(array(id => '',type =>'', action => '', cat => '', label => ''))
34
+ );
35
+ update_option( 'ga_events_options', $ga_events_options );
36
+
37
+ }
38
+
39
+ /*
40
+ * Plugin Admin Settings
41
+ */
42
+
43
+ add_action( 'admin_menu', 'ga_events_menu');
44
+
45
+ function ga_events_menu() {
46
+ add_options_page('WP Google Analytics Settings','WP Google Analytics Events','manage_options', __FILE__, 'ga_events_settings_page' );
47
+ }
48
+
49
+ function ga_events_settings_page() {
50
+ ?>
51
+ <div class="wrap">
52
+ <?php screen_icon( 'plugins' ); ?>
53
+ <h2>GA Scroll Events Plugin</h2>
54
+
55
+ <form method="post" action='options.php'>
56
+ <?php settings_fields('ga_events_options'); ?>
57
+ <?php do_settings_sections('ga_events'); ?>
58
+ <input class="button-primary" type="submit" name="submit" value="Save Changes" />
59
+ </form>
60
+
61
+ </div>
62
+
63
+ <?php
64
+ }
65
+
66
+ add_action('admin_init', 'ga_events_admin_init');
67
+
68
+ function ga_events_admin_init() {
69
+ register_setting('ga_events_options','ga_events_options','ga_events_validate');
70
+ add_settings_section('ga_events_main','WP Google Analytics Events Settings', 'ga_events_section_text','ga_events');
71
+ add_settings_field('ga_events_id', 'Google Analytics Identifier','ga_events_setting_input','ga_events','ga_events_main');
72
+ add_settings_field('ga_events_divs', 'Scroll Events','ga_events_setting_divs_input','ga_events','ga_events_main');
73
+ add_settings_field('ga_events_click', 'Click Events','ga_events_setting_click_input','ga_events','ga_events_main');
74
+
75
+
76
+ }
77
+
78
+ function ga_events_section_text() {
79
+ echo "<p>Enter your settings</p>";
80
+ }
81
+
82
+ function ga_events_setting_input() {
83
+ $options = get_option('ga_events_options');
84
+ $id = $options['id'];
85
+ echo "<input id='id' name='ga_events_options[id]' type='text' value='$id' />";
86
+
87
+ }
88
+
89
+ function ga_events_setting_divs_input() {
90
+ $options = get_option('ga_events_options');
91
+ $divs = $options['divs'];
92
+ echo "<table class='widefat'><thead><th>Element Name</th><th>Type</th><th>Event Category</th><th>Event Action</th><th>Event Label</th><th></th></thead><tbody>";
93
+ if (!($divs[0][0])){
94
+ echo "<tr>";
95
+ echo "<td><input id='divs' name='ga_events_options[divs][0][0]' type='text' value='".$divs[0][0]."' /></td>";
96
+ echo "<td><select id='divs' name='ga_events_options[divs][0][1]'><option selected value='id' >id</option><option value='class'>class</option></select></td>";
97
+ echo "<td><input id='divs' name='ga_events_options[divs][0][2]' type='text' value='".$divs[0][2]."' /></td>";
98
+ echo "<td><input id='divs' name='ga_events_options[divs][0][3]' type='text' value='".$divs[0][3]."' /></td>";
99
+ echo "<td><input id='divs' name='ga_events_options[divs][0][4]' type='text' value='".$divs[0][4]."' /></td>";
100
+ echo "</tr>";
101
+
102
+ }else{
103
+ for ($i = 0; $i < sizeof($divs)+1; $i++){
104
+
105
+ echo "<tr>";
106
+ echo "<td><input id='divs' name='ga_events_options[divs][$i][0] type='text' value='".$divs[$i][0]."' /></td>";
107
+ echo "<td><select id='divs' name='ga_events_options[divs][$i][1]'>";
108
+ if ($divs[$i][1] == 'id'){
109
+ echo "<option selected value='id' >id</option><option value='class'>class</option></select></td>";
110
+ }else {
111
+ echo "<option value='id' >id</option><option selected value='class'>class</option></select></td>";
112
+ }
113
+ echo "<td><input id='divs' name='ga_events_options[divs][$i][2]' type='text' value='".$divs[$i][2]."' /></td>";
114
+ echo "<td><input id='divs' name='ga_events_options[divs][$i][3]' type='text' value='".$divs[$i][3]."' /></td>";
115
+ echo "<td><input id='divs' name='ga_events_options[divs][$i][4]' type='text' value='".$divs[$i][4]."' /></td>";
116
+ echo "<td><a class='remove' href=''>Remove</a></td>";
117
+ echo "</tr>";
118
+
119
+ }
120
+
121
+ }
122
+ echo "</tbody></table>";
123
+ }
124
+
125
+
126
+ function ga_events_setting_click_input() {
127
+ $options = get_option('ga_events_options');
128
+ $click = $options['click'];
129
+ echo "<table class='widefat'><thead><th>Element Name</th><th>Type</th><th>Event Category</th><th>Event Action</th><th>Event Label</th><th></th></thead><tbody>";
130
+ if (!($click[0][0])){
131
+ echo "<tr>";
132
+ echo "<td><input id='click' name='ga_events_options[click][0][0]' type='text' value='".$click[0][0]."' /></td>";
133
+ echo "<td><select id='click' name='ga_events_options[click][0][1]'><option selected value='id'>id</option><option value='class'>class</option></select></td>";
134
+ echo "<td><input id='click' name='ga_events_options[click][0][2]' type='text' value='".$click[0][2]."' /></td>";
135
+ echo "<td><input id='click' name='ga_events_options[click][0][3]' type='text' value='".$click[0][3]."' /></td>";
136
+ echo "<td><input id='click' name='ga_events_options[click][0][4]' type='text' value='".$click[0][4]."' /></td>";
137
+ echo "</tr>";
138
+
139
+ }else{
140
+ for ($i = 0; $i < sizeof($click)+1; $i++){
141
+
142
+ echo "<tr>";
143
+ echo "<td><input id='divs' name='ga_events_options[click][$i][0] type='text' value='".$click[$i][0]."' /></td>";
144
+ echo "<td><select id='click' name='ga_events_options[click][$i][1]'>";
145
+ if ($click[$i][1] == 'id'){
146
+ echo "<option selected value='id' >id</option><option value='class'>class</option></select></td>";
147
+ }else {
148
+ echo "<option value='id' >id</option><option selected value='class'>class</option></select></td>";
149
+ }
150
+ echo "<td><input id='click' name='ga_events_options[click][$i][2] type='text' value='".$click[$i][2]."' /></td>";
151
+ echo "<td><input id='click' name='ga_events_options[click][$i][3] type='text' value='".$click[$i][3]."' /></td>";
152
+ echo "<td><input id='click' name='ga_events_options[click][$i][4] type='text' value='".$click[$i][4]."' /></td>";
153
+ echo "<td><a class='remove' href=''>Remove</a></td>";
154
+ echo "</tr>";
155
+
156
+ }
157
+
158
+ }
159
+ echo "</tbody></table>";
160
+ echo "<script>
161
+ jQuery('.remove').click(function (event) {
162
+ event.preventDefault();
163
+ jQuery(this).closest('tr').remove();
164
+ });
165
+ jQuery('.add').click(function (event) {
166
+ event.preventDefault();
167
+ });
168
+ </script>
169
+ ";
170
+
171
+ }
172
+
173
+
174
+ function ga_events_validate($form){
175
+ $updated = array();
176
+ $updated['id'] = $form['id'];
177
+
178
+ for ($i = 0, $j = 0; $i< sizeof($form['divs']); $i++){
179
+ if ($form['divs'][$i][0]){
180
+ $updated['divs'][$j] = $form['divs'][$i];
181
+ $j++;
182
+ }
183
+ }
184
+
185
+ for ($i = 0, $j = 0; $i< sizeof($form['click']); $i++){
186
+ if ($form['click'][$i][0]){
187
+ $updated['click'][$j] = $form['click'][$i];
188
+ $j++;
189
+ }
190
+ }
191
+
192
+ return $updated;
193
+ }
194
+
195
+
196
+ /*
197
+ * Init
198
+ */
199
+
200
+ add_action('init','ga_events_scripts');
201
+
202
+ function ga_events_scripts() {
203
+ wp_enqueue_script('jquery');
204
+ wp_enqueue_script('scrolldepth',plugins_url( '/js/ga-scroll-events.js' , __FILE__ ));
205
+ }
206
+
207
+
208
+ add_action( 'plugins_loaded', 'ga_events_setup');
209
+
210
+ function ga_events_setup() {
211
+ add_action( 'wp_head', 'ga_events_header', 100 );
212
+ }
213
+
214
+
215
+ function ga_events_header() {
216
+ $options = get_option('ga_events_options');
217
+ $id = $options['id'];
218
+ $domain = $_SERVER['SERVER_NAME'];
219
+
220
+ echo "<script>
221
+ (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
222
+ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
223
+ m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
224
+ })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
225
+
226
+ ga('create', '$id', '$domain');
227
+ ga('send', 'pageview');
228
+
229
+ </script>
230
+
231
+ <script type='text/javascript'>
232
+
233
+ var _gaq = _gaq || [];
234
+ _gaq.push(['_setAccount', '$id']);
235
+ _gaq.push(['_trackPageview']);
236
+
237
+ (function() {
238
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
239
+ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
240
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
241
+ })();
242
+
243
+ </script>";
244
+
245
+ }
246
+
247
+ add_action( 'wp_footer', 'ga_events_footer', 100 );
248
+
249
+ function ga_events_footer() {
250
+ $options = get_option('ga_events_options');
251
+ $divs = $options['divs'];
252
+ $click = $options['click'];
253
+ echo "
254
+ <script>
255
+
256
+ jQuery(document).ready(function() {
257
+ scroll_events.bind_events( {
258
+ scroll_elements: [";
259
+ $i = 0;
260
+ if (is_array($divs)){
261
+ foreach ($divs as $div) {
262
+ if ($i == 0) {
263
+ echo ga_events_get_selector($div);
264
+ }else{
265
+ echo ",".ga_events_get_selector($div);
266
+ }
267
+ $i++;
268
+ }
269
+ }
270
+
271
+ echo "],
272
+ click_elements: [";
273
+ $i = 0;
274
+ if (is_array($click)){
275
+ foreach ($click as $cl) {
276
+ if ($i == 0) {
277
+ echo ga_events_get_selector($cl);
278
+ }else{
279
+ echo ",".ga_events_get_selector($cl);
280
+ }
281
+ $i++;
282
+ }
283
+ }
284
+ echo "],
285
+ });
286
+ });
287
+
288
+ </script>";
289
+
290
+
291
+ }
292
+
293
+ function ga_events_get_selector($element) {
294
+ if ($element[0]){
295
+ $selector = "{'select':'";
296
+ $selector .= ($element[1] =='class') ? '.':'#';
297
+ $selector .= str_replace(' ','',$element[0])."',";
298
+ $selector .= "'category':'".$element[2]."',";
299
+ $selector .= "'action':'".$element[3]."',";
300
+ $selector .= "'label':'".$element[4]."'";
301
+ $selector .= '}';
302
+ return $selector;
303
+ }else{
304
+ return '';
305
+ }
306
+
307
+ }
308
+
309
+ ?>
js/ga-scroll-events.js ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*!
2
+ * WP Googel Analytics Events | v1.0
3
+ * Copyright (c) 2013 Yuval Oren (@yuvalo)
4
+ * License: GPLv2
5
+ */
6
+
7
+ /*jslint indent: 4 */
8
+ /*global $, jQuery, document, window, _gaq*/
9
+
10
+ var scroll_events = (function ($) {
11
+ "use strict";
12
+
13
+ var scroll_elements = [];
14
+ var click_elements = [];
15
+
16
+ var track_event = function (category, action, label) {
17
+ var event_category = !category ? '' : category;
18
+ var event_action = !action ? '' : action;
19
+ var event_label = !label ? '' : label;
20
+ _gaq.push(['_trackEvent', category, action, label]);
21
+ };
22
+
23
+ var click_event = function (event) {
24
+ track_event(event.data.category, event.data.action, event.data.label);
25
+ };
26
+
27
+ return {
28
+ bind_events : function (settings) {
29
+ scroll_elements = settings.scroll_elements;
30
+ click_elements = settings.click_elements;
31
+ var i;
32
+ for (i = 0; i < click_elements.length; i++) {
33
+ var clicked = click_elements[i];
34
+ $(clicked.select).on('click', clicked, click_event);
35
+ }
36
+
37
+
38
+
39
+ $(window).scroll(function () {
40
+ var ga_window = $(window).height();
41
+ var ga_scroll_top = $(document).scrollTop();
42
+ var i;
43
+ for (i = 0; i < scroll_elements.length; i++) {
44
+ if (!scroll_elements[i].sent) {
45
+ scroll_elements[i].offset = $(scroll_elements[i].select).offset();
46
+ if (scroll_elements[i].offset && ga_scroll_top + ga_window >= scroll_elements[i].offset.top + $(scroll_elements[i].select).height()) {
47
+ track_event(scroll_elements[i].category, scroll_elements[i].action, scroll_elements[i].label);
48
+ scroll_elements[i].sent = true;
49
+ }
50
+ }
51
+ }
52
+ });
53
+ }
54
+ };
55
+
56
+ }(jQuery));
readme.txt ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === WP Google Analytics Events ===
2
+ Contributors: yuvalo
3
+ Tags: analytics, google, events, ga
4
+ Tested up to: 3.6
5
+ Stable tag: 1.1
6
+ License: GPLv2
7
+ Requires at least: 3.0
8
+
9
+ Adds the Google Analytics code to your website and enables you to send events on scroll or click.
10
+ == Description ==
11
+ The WP Google Analytics Events plugin allows you to send events to Google Analytics for scrolling or clicking elements on the page.
12
+ For example, create a Google Analytics event when a visitor scrolled down and viewed your pricing table. Did a user play a video? Create an event for that.
13
+ == Installation ==
14
+ Add the plugin by uploading the zip file or by installing it from the Wordpress pugin directory.
15
+ Activate the plugin and go to the WP Google Analytics Settings page.
16
+ Fill in your Google Analytics account identifier, add scroll and start creating scroll and click events.
17
+
18
+ == Screenshots ==
19
+ 1. Plugin Options screenshot-1.png
20
+ 2. Google Analytics real time events screenshot-2.png
21
+ == Changelog == = Version 1.0 =
screenshot-1.png ADDED
Binary file
screenshot-2.png ADDED
Binary file
uninstall.php ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ <?php
2
+ if( !defined( 'WP_UNINSTALL_PLUGIN' ) )
3
+ exit ();
4
+
5
+ delete_option( 'ga_scroll_events_install' );
6
+ ?>