forked from segment-boneyard/analytics-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attribution-wordpress.php
1085 lines (910 loc) · 32.1 KB
/
attribution-wordpress.php
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: Attribution for Wordpress
Plugin URI: https://attributionapp.com
Description: Attribution for WordPress lets you easily track advertising spend, revenue and conversions across all of your marketing channels.
Version: 1.0.3
License: GPLv2
Author: Attribution
Author URI: https://attributionapp.com
*/
class Attribution_Analytics {
/**
* The singleton instance of Attribution_Analytics.
*
* @access private
* @var Attribution_Analytics
* @since 1.0.0
*/
private static $instance;
/**
* Retrieves the one true instance of Attribution_Analytics
* Also sets up constants and includes deprecated files.
*
* @since 1.0.0
* @return object Singleton instance of Attribution_Analytics
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new Attribution_Analytics;
self::$instance->setup_constants();
}
return self::$instance;
}
/**
* Sets up constants for file paths, folders, URLs and directory names related to the plugin.
*
* @since 1.0.0
*/
public function setup_constants() {
// Set the core file path
define( 'ATTR_FILE_PATH', dirname( __FILE__ ) );
// Define the path to the plugin folder
define( 'ATTR_DIR_NAME', basename( ATTR_FILE_PATH ) );
// Define the URL to the plugin folder
define( 'ATTR_FOLDER', dirname( plugin_basename( __FILE__ ) ) );
define( 'ATTR_URL' , plugins_url( '', __FILE__ ) );
}
/**
* Render the Attribution Javascript snippet.
*
* @since 1.0.0
*
* @param array $settings Settings options array.
* @param bool $ignore Whether or not to ignore the call and avoid outputting the API key snippet.
*/
public static function initialize( $settings, $ignore = false ) {
if ( ! isset( $settings['api_key'] ) || $settings['api_key'] == '' ) {
return;
}
include_once( ATTR_FILE_PATH . '/templates/snippet.php' );
}
/**
* Render a Javascript `identify` call
*
* @since 1.0.0
*
* @param int|string $user_id Current User ID.
* Generated via get_current_user_id() if logged in, anonymous user ID if not.
* @param array $traits Array of traits to pass to Attribution.
* @param array $options Array of options to pass to Attribution.
*/
public static function identify( $user_id, $traits = array(), $options = array() ) {
// Set the proper `library` option so we know where the API calls come from.
$options['library'] = 'attribution-wordpress';
include_once( ATTR_FILE_PATH. '/templates/identify.php' );
}
/**
* Render a Javascript `track` call
*
* @since 1.0.0
*
* @param string $event The name of the event to pass to Attribution.
* @param array $properties An array of properties to pass to Attribution.
* @param array $options An array of options to pass to Attribution.
* @param boolean $http_event Whether or not the event is occurring over HTTP, as opposed to on page load.
* This is helpful to track events that occur between page loads, like commenting.
*
*/
public static function track( $event, $properties = array(), $options = array(), $http_event = false ) {
// Set the proper `library` option so we know where the API calls come from.
$options['library'] = 'attribution-wordpress';
include_once( ATTR_FILE_PATH . '/templates/track.php' );
}
/**
* Render a Javascript `page` call
*
* @since 1.0.0
*
* @param string $category Category (or name) of event
* @param string $name Optional, but if set, category must be set as well.
* @param array $properties An array of properties to pass to Attribution.
* @param array $options An array of options to pass to Attribution.
* @param boolean $http_event Whether or not the event is occurring over HTTP, as opposed to on page load.
* This is helpful to track events that occur between page loads, like commenting.
*/
public static function page( $category = '', $name = '', $properties = array(), $options = array(), $http_event = false ) {
// Set the proper `library` option so we know where the API calls come from.
$options['library'] = 'attribution-wordpress';
include_once( ATTR_FILE_PATH . '/templates/page.php' );
}
/**
* Creates an alias between an anonymous ID and a newly created user ID.
* Primarily used for MixPanel.
*
* @since 1.0.0
*
* @param int|string $from The anonymous ID that we're aliasing from.
* @param int|string $to The newly created User ID we are aliasing to.
* @param string $context Optional context parameter to be passed to Attribution.
*/
public static function alias( $from, $to, $context = '' ) {
include_once( ATTR_FILE_PATH . '/templates/alias.php' );
}
}
class Attribution_Analytics_WordPress {
/**
* Slug used in page and menu names
*/
const SLUG = 'attribution';
/**
* Current plugin version.
*/
const VERSION = '1.0.3';
/**
* The singleton instance of Attribution_Analytics_WordPress.
*
* @access private
* @var Attribution_Analytics_WordPress
* @since 1.0.0
*/
private static $instance;
/**
* The singleton instance of Attribution_Analytics, for use in our class.
*
* @access private
* @var Attribution_Analytics
* @since 1.0.0
*/
private $analytics;
/**
* The name of our options array.
*
* @access private
* @var string
* @since 1.0.0
*/
private $option = 'attribution_wordpress_options';
/**
* The default values for our options array.
*
* Not used since 1.0.0, outside of activation hooks, with our move to the Settings API.
* See Attribution_Analytics_WordPress::register_settings().
*
* @access public
* @var array
* @since 1.0.0
*/
public $defaults = array(
// Your Attribution API key that we'll use to initialize attibution.js.
'api_key' => '',
// Whether or not we should ignore users of above a certain permissions
// level. (eg. `11` ignores nobody and `8` ignores Administrators)
'ignore_user_level' => 11,
// Whether or not we should track events for posts. This also includes
// custom post types, for example a Product post type.
'track_posts' => 0,
// Whether or not we should track events for pages. This includes the
// Home page and things like the About page, Contact page, etc.
'track_pages' => 0,
// Whether or not we should track custom events for archive pages like
// the Category archive or the Author archive.
'track_archives' => 0,
// Whether or not we should track custom events for comments
'track_comments' => 0,
// Whether or not we should track custom events for searches
'track_searches' => 0,
// Whether or not we should track custom events for users logging in
'track_logins' => 0,
// Whether or not we should track custom events for viewing the logged in page.
'track_login_page' => false,
// Whether or not we should track custom events for the Search page.
'exclude_custom_post_types' => array(),
);
/**
* Retrieves the one true instance of Attribution_Analytics_WordPress
*
* @since 1.0.0
* @return object Singleton instance of Attribution_Analytics_WordPress
*/
public static function get_instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Attribution_Analytics_WordPress ) ) {
self::$instance = new Attribution_Analytics_WordPress;
self::$instance->load_textdomain();
self::$instance->admin_hooks();
self::$instance->frontend_hooks();
self::$instance->analytics = Attribution_Analytics::get_instance();
self::$instance->include_files();
}
return self::$instance;
}
/**
* Returns Settings option name.
*
* @since 1.0.0
*
* @return string Settings option name
*/
public function get_option_name() {
return $this->option;
}
/**
* Hooks into actions and filters that affect the administration areas.
*
* @since 1.0.0
*/
public function admin_hooks() {
add_action( 'admin_menu' , array( self::$instance, 'admin_menu' ) );
add_filter( 'plugin_action_links', array( self::$instance, 'plugin_action_links' ), 10, 2 );
add_filter( 'plugin_row_meta' , array( self::$instance, 'plugin_row_meta' ) , 10, 2 );
add_action( 'admin_init' , array( self::$instance, 'register_settings' ) );
}
/**
* Includes core classes.
* Currently includes Attribution_Cookie and eCommerce bootstrap.
*
* @uses do_action() Allows other plugins to hook in before or after everything is bootstrapped.
*
* @since 1.0.0
*/
public function include_files() {
do_action( 'attribution_pre_include_files', self::$instance );
include_once( ATTR_FILE_PATH . '/includes/class.attribution-settings.php' );
include_once( ATTR_FILE_PATH . '/includes/class.attribution-cookie.php' );
include_once( ATTR_FILE_PATH . '/integrations/ecommerce.php' );
do_action( 'attribution_include_files', self::$instance );
}
/**
* Hooks into actions and filters that affect the front-end.
* That is to say, this is where the magic happens.
*
* @since 1.0.0
*/
public function frontend_hooks() {
add_action( 'wp_head' , array( self::$instance, 'wp_head' ) , 9 );
add_action( 'admin_head' , array( self::$instance, 'wp_head' ) , 9 );
add_action( 'login_head' , array( self::$instance, 'wp_head' ) , 9 );
add_action( 'wp_footer' , array( self::$instance, 'wp_footer' ) , 9 );
add_action( 'login_footer' , array( self::$instance, 'wp_footer' ) , 9 );
add_action( 'admin_footer' , array( self::$instance, 'wp_footer' ) , 9 );
add_action( 'wp_insert_comment', array( self::$instance, 'insert_comment' ), 9, 2 );
add_action( 'wp_login' , array( self::$instance, 'login_event' ), 9, 2 );
add_action( 'user_register' , array( self::$instance, 'user_register' ), 9 );
}
/**
* Returns array of settings.
*
* @since 1.0.4
*/
public function _get_default_settings() {
return apply_filters( 'attribution_default_settings', array(
'general' => array(
'title' => __( 'General', 'attribution' ),
'callback' => array( 'Attribution_Settings', 'general_section_callback' ),
'fields' => array(
array(
'name' => 'api_key',
'title' => __( 'Attribution Project ID', 'attribution' ),
'callback' => array( 'Attribution_Settings', 'api_key_callback' ),
)
)
),
'advanced' => array(
'title' => __( 'Advanced Settings', 'attribution' ),
'callback' => array( 'Attribution_Settings', 'advanced_section_callback' ),
'fields' => array(
array(
'name' => 'ignore_user_level',
'title' => __( 'Users to Ignore', 'attribution' ),
'callback' => array( 'Attribution_Settings', 'ignore_user_level_callback' ),
),
array(
'name' => 'track_posts',
'title' => __( 'Track Posts', 'attribution' ),
'callback' => array( 'Attribution_Settings', 'track_posts_callback' ),
),
array(
'name' => 'exclude_post_types',
'title' => __( 'Exclude Post Types', 'attribution' ),
'callback' => array( 'Attribution_Settings', 'exclude_custom_post_types' ),
),
array(
'name' => 'track_pages',
'title' => __( 'Track Pages', 'attribution' ),
'callback' => array( 'Attribution_Settings', 'track_pages_callback' ),
),
array(
'name' => 'track_archives',
'title' => __( 'Track Archives', 'attribution' ),
'callback' => array( 'Attribution_Settings', 'track_archives_callback' ),
),
array(
'name' => 'track_archives',
'title' => __( 'Track Archives', 'attribution' ),
'callback' => array( 'Attribution_Settings', 'track_archives_callback' ),
),
array(
'name' => 'track_comments',
'title' => __( 'Track Comments', 'attribution' ),
'callback' => array( 'Attribution_Settings', 'track_comments_callback' ),
),
array(
'name' => 'track_logins',
'title' => __( 'Track Logins', 'attribution' ),
'callback' => array( 'Attribution_Settings', 'track_logins_callback' ),
),
array(
'name' => 'track_login_page',
'title' => __( 'Track Login Page Views', 'attribution' ),
'callback' => array( 'Attribution_Settings', 'track_login_page_callback' ),
),
array(
'name' => 'track_searches',
'title' => __( 'Track Searches', 'attribution' ),
'callback' => array( 'Attribution_Settings', 'track_search_callback' ),
),
)
),
)
);
}
/**
* Registers our settings, fields and sections using the WordPress Settings API.
*
* Developers should use the `attribution_default_settings` filter to add settings.
* They should also use the `attribution_settings_core_validation` filter to validate
* any settings they add.
*
* @since 1.0.0
* @return void
*/
public function register_settings() {
$settings = $this->_get_default_settings();
register_setting( self::SLUG, $this->get_option_name(), array( 'Attribution_Settings', 'core_validation' ) );
foreach ( $settings as $section_name => $section ) {
add_settings_section(
$section_name,
$section['title'],
$section['callback'],
self::SLUG
);
foreach ( $section['fields'] as $field ) {
add_settings_field(
$field['name'],
$field['title'],
$field['callback'],
self::SLUG,
$section_name
);
}
}
}
/**
* Empty constructor, as we prefer to get_instance().
*
* @since 1.0.0
*
*/
public function __construct() {}
/**
* Loads the properly localized PO/MO files
*
* @since 1.0.0
*/
public function load_textdomain() {
// Set filter for plugin's languages directory
$attribution_lang_dir = dirname( plugin_basename( __FILE__ ) ) . '/languages/';
$attribution_lang_dir = apply_filters( 'attribution_languages_directory', $attribution_lang_dir );
// Traditional WordPress plugin locale filter
$locale = apply_filters( 'plugin_locale', get_locale(), 'attribution' );
$mofile = sprintf( '%1$s-%2$s.mo', 'attribution', $locale );
// Setup paths to current locale file
$mofile_local = $attribution_lang_dir . $mofile;
$mofile_global = WP_LANG_DIR . '/attribution/' . $mofile;
if ( file_exists( $mofile_global ) ) {
// Look in global /wp-content/languages/attribution folder
load_textdomain( 'attribution', $mofile_global );
} elseif ( file_exists( $mofile_local ) ) {
// Look in local /wp-content/plugins/analytics-wordpress/languages/ folder
load_textdomain( 'attribution', $mofile_local );
} else {
// Load the default language files
load_plugin_textdomain( 'attribution', false, $attribution_lang_dir );
}
}
/**
* Outputs analytics javascript and analytics.identify() snippet in head for admin, login page and wp_head.
*
* @since 1.0.0
*/
public function wp_head() {
// Figure out whether the user should be ignored or not.
$ignore = false;
$settings = $this->get_settings();
$user = wp_get_current_user();
if ( $user->user_level >= $settings['ignore_user_level'] ) {
$ignore = true;
}
// Render the snippet.
self::$instance->analytics->initialize( $settings, $ignore );
}
/**
* Outputs analytics.track()/.page()/ snippet in head for admin, login page and wp_footer.
*
* @since 1.0.0
*/
public function wp_footer() {
// Identify the user if the current user merits it.
$identify = $this->get_current_user_identify();
if ( $identify ) {
if ( ! isset( $identify['options'] ) ) {
$identify['options'] = array();
}
self::$instance->analytics->identify( $identify['user_id'], $identify['traits'], $identify['options'] );
}
// Track a custom page view event if the current page merits it.
$track = $this->get_current_page_track();
$page = $this->get_current_page();
if ( $track ) {
$http_event = isset( $track['http_event'] ) ? $track['http_event'] : false;
self::$instance->analytics->track( $track['event'], $track['properties'], array(), $http_event );
}
if ( $page ) {
self::$instance->analytics->page( $page['page'], $page['properties'] );
}
}
/**
* Uses Attribution_Cookie::set_cookie() to notify Attribution that a comment has been left.
*
* @param int $id Comment ID. Unused.
* @param object $comment WP_Comment object Unused.
*
* @since 1.0.0
*/
public function insert_comment( $id, $comment ) {
Attribution_Cookie::set_cookie( 'left_comment', md5( json_encode( wp_get_current_commenter() ) ) );
}
/**
* Uses Attribution_Cookie::set_cookie() to notify Attribution that a user has logged in.
*
* @since 1.0.0
*
* @param string $login Username of logged in user.
* @param WP_User $user User object of logged in user.
*
*/
public function login_event( $login, $user ) {
Attribution_Cookie::set_cookie( 'logged_in', md5( json_encode( $user ) ) );
}
/**
* Uses Attribution_Cookie::set_cookie() to notify Attribution that a user has signed up.
*
* @since 1.0.0
*
* @param int $user_id Username of new user.
*
*/
public function user_register( $user_id ) {
Attribution_Cookie::set_cookie( 'signed_up', json_encode( $user_id ) );
}
/**
* Adds "Settings" link to plugin row.
*
* @param array $links Array of links on plugin action row.
* @param string $file Basename of file.
* @return array $links Modified array of links on plugin action row.
*/
public function plugin_action_links( $links, $file ) {
// Not for other plugins, silly. NOTE: This doesn't work properly when
// the plugin for testing is a symlink!! If you change this, test it.
// Note: Works fine as of 3.9, see @link: https://core.trac.wordpress.org/ticket/16953
if ( $file != plugin_basename( __FILE__ ) ) {
return $links;
}
// Add settings link to the beginning of the row of links.
$settings_link = sprintf( '<a href="options-general.php?page=' . self::SLUG . '">%s</a>', __( 'Settings' ) );
array_unshift( $links, $settings_link );
return $links;
}
/**
* Adds Settings and Documentation links to plugin row meta.
*
* @since 1.0.0
*
* @param array $plugin_meta An array of the plugin's metadata,
* including the version, author,
* author URI, and plugin URI.
* @param string $plugin_file Path to the plugin file, relative to the plugins directory.
*
* @return array Modified array of plugin metadata.
*/
public function plugin_row_meta( $plugin_meta, $plugin_file ) {
// Not for other plugins, silly. NOTE: This doesn't work properly when
// the plugin for testing is a symlink!! If you change this, test it.
// Note: Works fine as of 3.9, see @link: https://core.trac.wordpress.org/ticket/16953
if ( $plugin_file != plugin_basename( __FILE__ ) ) {
return $plugin_meta;
}
// Add a settings and docs link to the end of the row of links row of links.
$settings_link = sprintf( '<a href="options-general.php?page=' . self::SLUG . '">%s</a>', __( 'Settings' ) );
$docs_link = sprintf( '<a href="https://attribution.readme.io/docs/wordpress" target="_blank">%s</a>', __( 'Docs', 'attribution' ) );
array_push( $plugin_meta, $settings_link, $docs_link );
return $plugin_meta;
}
/**
* Adds "Analytics" Menu item to admin area.
*
* @since 1.0.0
*/
public function admin_menu() {
add_options_page(
apply_filters( 'attribution_admin_menu_page_title', __( 'Attribution', 'attribution' ) ), // Page Title
apply_filters( 'attribution_admin_menu_menu_title', __( 'Attribution', 'attribution' ) ), // Menu Title
apply_filters( 'attribution_admin_settings_capability', 'manage_options' ), // Capability Required
self::SLUG, // Menu Slug
array( $this, 'admin_page' ) // Function
);
}
/**
* The callback used to build out the admin settings area.
*
* @since 1.0.0
*/
public function admin_page() {
// Make sure the user has the required permissions to view the settings.
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( __( 'Sorry, you don\'t have the permissions to access this page.', 'attribution' ) );
}
include_once( ATTR_FILE_PATH . '/templates/settings.php');
}
/**
* Retrieves settings array.
*
* @since 1.0.0
*
* @uses apply_filters() Applies 'attribution_get_settings' filter to allow other developers to override.
*
* @return array Array of settings.
*/
public function get_settings() {
return apply_filters( 'attribution_get_settings', get_option( $this->option ), $this );
}
/**
* Updates settings array.
*
* @since 1.0.0
*
* @param array $settings Array of settings
* @uses apply_filters() Applies 'attribution_get_settings' filter to allow other developers to override.
*
* @deprecated Deprecated in 1.0.0
*
* @return array Array of settings.
*/
private function set_settings( $settings ) {
return update_option( $this->option, $settings );
}
/**
* Based on the current user or commenter, see if we have enough information
* to record an `identify` call. Since commenters don't have IDs, we
* identify everyone by their email address.
*
* @since 1.0.0
*
* @return bool|array Returns false if there is no commenter or logged in user
* An array of the user ID and traits if there is an authenticated user.
*/
public function get_current_user_identify() {
$settings = $this->get_settings();
$user = wp_get_current_user();
$commenter = array_filter( wp_get_current_commenter() );
$identify = false;
// We've got a logged-in user.
// http://codex.wordpress.org/Function_Reference/wp_get_current_user
if ( is_user_logged_in() && $user ) {
$identify = array(
'user_id' => $user->user_email,
'traits' => array(
'username' => $user->user_login,
'email' => $user->user_email,
'firstName' => $user->user_firstname,
'lastName' => $user->user_lastname,
'url' => $user->user_url
)
);
}
// We've got a commenter.
// http://codex.wordpress.org/Function_Reference/wp_get_current_commenter
else if ( $commenter ) {
$identify = array(
'user_id' => $commenter['comment_author_email'],
'traits' => array(
'email' => $commenter['comment_author_email'],
'name' => $commenter['comment_author'],
'url' => $commenter['comment_author_url']
)
);
}
if ( $identify ) {
// Clean out empty traits before sending it back.
$identify['traits'] = array_filter( $identify['traits'] );
}
/**
* Allows developers to modify the entire $identify call.
*
* @since 1.0.0
*/
return apply_filters( 'attribution_get_current_user_identify', $identify, $settings, $this );
}
/**
* Used to track the current event. Used for analytics.track().
*
* @since 1.0.0
*
* @return array Array containing the page being tracked along with any additional properties.
*/
private function get_current_page_track() {
$settings = $this->get_settings();
// Login Event
// --------
if ( $settings['track_logins'] ) {
$user = wp_get_current_user();
$hash = md5( json_encode( $user ) );
if ( Attribution_Cookie::get_cookie( 'logged_in', $hash ) ) {
$track = array(
'event' => __( 'Logged In', 'attribution' ),
'properties' => array(
'username' => $user->user_login,
'email' => $user->user_email,
'name' => $user->display_name,
'firstName' => $user->user_firstname,
'lastName' => $user->user_lastname,
'url' => $user->user_url
),
'http_event' => 'logged_in'
);
}
}
// Posts
// -----
if ( $settings['track_posts'] ) {
// A post or a custom post. `is_single` also returns attachments, so
// we filter those out. The event name is based on the post's type,
// and is uppercased.
if ( is_single() && ! is_attachment() ) {
if ( ! self::is_excluded_post_type() ) {
$categories = implode( ', ', wp_list_pluck( get_the_category( get_the_ID() ), 'name' ) );
$track = array(
'event' => sprintf( __( 'Viewed %s', 'attribution' ), ucfirst( get_post_type() ) ),
'properties' => array(
'title' => single_post_title( '', false ),
'category' => $categories
)
);
}
}
}
// Pages
// -----
if ( $settings['track_pages'] ) {
// The front page of their site, whether it's a page or a list of
// recent blog entries. `is_home` only works if it's not a page,
// that's why we don't use it.
if ( is_front_page() ) {
$track = array(
'event' => __( 'Viewed Home Page', 'attribution' )
);
}
// A normal WordPress page.
else if ( is_page() ) {
$track = array(
'event' => sprintf( __( 'Viewed %s Page', 'attribution' ), single_post_title( '', false ) ),
);
}
}
// Archives
// --------
if ( $settings['track_archives'] ) {
// An author archive page. Check the `wp_title` docs to see how they
// get the title of the page, cuz it's weird.
// http://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/general-template.php#L0
if ( is_author() ) {
$author = get_queried_object();
$track = array(
'event' => __( 'Viewed Author Page', 'attribution' ),
'properties' => array(
'author' => $author->display_name
)
);
}
// A tag archive page. Use `single_tag_title` to get the name.
// http://codex.wordpress.org/Function_Reference/single_tag_title
else if ( is_tag() ) {
$track = array(
'event' => __( 'Viewed Tag Page', 'attribution' ),
'properties' => array(
' tag' => single_tag_title( '', false )
)
);
}
// A category archive page. Use `single_cat_title` to get the name.
// http://codex.wordpress.org/Function_Reference/single_cat_title
else if ( is_category() ) {
$track = array(
'event' => __( 'Viewed Category Page', 'attribution' ),
'properties' => array(
'category' => single_cat_title( '', false )
)
);
}
}
// Comments
// --------
if ( $settings['track_comments'] ) {
$commenter = array_filter( wp_get_current_commenter() );
if ( $commenter ) {
$hash = md5( json_encode( $commenter ) );
if ( Attribution_Cookie::get_cookie( 'left_comment', $hash ) ) {
$track = array(
'event' => __( 'Commented', 'attribution' ),
'properties' => array(
'commenter' => $commenter
),
'http_event' => 'left_comment'
);
}
}
}
// Login Page
// --------
if ( $settings['track_login_page'] ) {
if ( did_action( 'login_init' ) ) {
$track = array(
'event' => __( 'Viewed Login Page', 'attribution' )
);
}
}
// Searches
// --------
if ( $settings['track_searches'] ) {
// The search page.
if ( is_search() ) {
$track = array(
'event' => __( 'Viewed Search Page', 'attribution' ),
'properties' => array(
'query' => get_query_var( 's' )
)
);
}
}
if ( Attribution_Cookie::get_cookie( 'signed_up' ) ) {
$user_id = json_decode( Attribution_Cookie::get_cookie( 'signed_up' ) );
$user = get_user_by( 'id', $user_id );
add_filter( 'attribution_get_current_user_identify', array( self::$instance, 'new_user_identify' ) );
$track = array(
'event' => __( 'User Signed Up', 'attribution' ),
'properties' => array(
'username' => $user->user_login,
'email' => $user->user_email,
'name' => $user->display_name,
'firstName' => $user->user_firstname,
'lastName' => $user->user_lastname,
'url' => $user->user_url
),
'http_event' => 'signed_up'
);
}
// We don't have a page we want to track.
if ( ! isset( $track ) ) {
$track = false;
}
if ( $track ) {
// All of these are checking for pages, and we don't want that to throw
// off Google Analytics's bounce rate, so mark them `noninteraction`.
$track['properties']['noninteraction'] = true;
// Clean out empty properties before sending it back.
$track['properties'] = array_filter( $track['properties'] );
}
return apply_filters( 'attribution_get_current_page_track', $track, $settings, $this );
}
/**
* Filters the .identify() call with the newly signed up user.
* This is helpful, as the user will often times not be authenticated after signing up.
*
* @since 1.0.0
*
* @param mixed $identify False if no user is found, array of traits and ID if a user is found.
* @return array $identify Array of traits for newly signed up user.
*/
public function new_user_identify( $identify ) {
if ( Attribution_Cookie::get_cookie( 'signed_up' ) ) {
$user_id = json_decode( Attribution_Cookie::get_cookie( 'signed_up' ) );
$user = get_user_by( 'id', $user_id );
$identify = array(
'user_id' => $user->user_email,
'traits' => array(
'username' => $user->user_login,
'email' => $user->user_email,
'firstName' => $user->user_firstname,
'lastName' => $user->user_lastname,
'url' => $user->user_url
)
);
}
return $identify;
}
/**
* Used to track the current page. Used for analytics.page().
* Unlike get_current_page_track(), we use this primarily as a pub-sub observer for other core events.
* This makes it much more manageable for other developers to hook and unhook from it as needed.
*
* @since 1.0.0
*
* @return array Array containing the page being tracked along with any additional properties.
*/
private function get_current_page() {
$page = apply_filters( 'attribution_get_current_page', false, $this->get_settings(), $this );
if ( $page ) {
$page['properties'] = is_array( $page['properties'] ) ? $page['properties'] : array();
// All of these are checking for pages, and we don't want that to throw