This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcandela-analytics.php
102 lines (91 loc) · 2.94 KB
/
candela-analytics.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
<?php
/**
* @package Candela Analytics
* @version 0.1
*/
/*
Plugin Name: Candela Analytics
Plugin URI: http://lumenlearning.com/
Description: Adds Google Analyics tracking code to the theme header. This plugin assumes that you will set CANDELA_ANALYTICS_WEB_PROPERTY_ID and CANDELA_ANALYTICS_COOKIE_DOMAIN in wp-config.php.
Version: 0.1
Author URI: http://lumenlearning.com
*/
//
add_action('wp_head', 'candela_analytics_script');
define('CANDELA_ANALYTICS_USERMETA_UUID', 'candela_analytics_uuid');
define('CANDELA_ANALYTICS_UUID_LENGTH', 32);
function candela_analytics_script() {
if ( defined( 'CANDELA_ANALYTICS_WEB_PROPERTY_ID' ) && defined( 'CANDELA_ANALYTICS_COOKIE_DOMAIN' ) ) {
candela_analytics_header();
candela_analytics_custom();
candela_analytics_footer();
}
}
function candela_analytics_header() {
print "<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
";
}
function candela_analytics_footer() {
print "\n</script>\n";
}
/**
* Outputs customized google analytics code.
*/
function candela_analytics_custom() {
print "ga('create', '" . CANDELA_ANALYTICS_WEB_PROPERTY_ID . "', '" . CANDELA_ANALYTICS_COOKIE_DOMAIN . "');\n";
print "ga('send', 'pageview');\n";
$uuid = candela_analytics_get_current_user_uuid();
if (!empty($uuid)) {
print "ga('set', '&uid', '" . $uuid . "');\n";
}
}
/**
* Return a uuid for the current user or the empty string if the user is not
* logged in. This has the side effect of creating a uuid for the current user
* if one does not exist.
*/
function candela_analytics_get_current_user_uuid() {
if (is_user_logged_in()) {
$current_user = wp_get_current_user();
switch_to_blog(1);
$uuid = get_user_meta( $current_user->ID, CANDELA_ANALYTICS_USERMETA_UUID, TRUE);
restore_current_blog();
if (empty($uuid)) {
$uuid = candela_analytics_next_uuid();
switch_to_blog(1);
update_user_meta( $current_user->ID, CANDELA_ANALYTICS_USERMETA_UUID, $uuid);
restore_current_blog();
}
return $uuid;
}
return '';
}
/**
* Find a suitable next uuid.
*/
function candela_analytics_next_uuid() {
$uuid = base64_encode(openssl_random_pseudo_bytes(CANDELA_ANALYTICS_UUID_LENGTH));
while (candela_analytics_uuid_exists($uuid)) {
$uuid = base64_encode(openssl_random_pseudo_bytes(CANDELA_ANALYTICS_UUID_LENGTH));
}
return $uuid;
}
/**
* Returns true if the given uuid exists for any user.
*/
function candela_analytics_uuid_exists($uuid) {
switch_to_blog(1);
$users = get_users(array(
'meta_key' => CANDELA_ANALYTICS_USERMETA_UID,
'meta_value' => $uuid,
));
restore_current_blog();
if (empty($users)) {
return FALSE;
}
return TRUE;
}