-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathScript_Loader.php
More file actions
executable file
·122 lines (103 loc) · 3.79 KB
/
Script_Loader.php
File metadata and controls
executable file
·122 lines (103 loc) · 3.79 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
<?php
/**
* @package koko-analytics
* @license GPL-3.0+
* @author Danny van Kooten
*/
namespace KokoAnalytics;
use KokoAnalytics\Normalizers\Path;
class Script_Loader
{
// WARNING: is used in Koko Analytics Pro (in its static form)
public static function get_request_path(): string
{
return Path::normalize($_SERVER["REQUEST_URI"] ?? '');
}
public function hook(): void
{
add_action('wp_head', [$this, 'print_js_object'], 1, 0);
add_action('wp_footer', [$this, 'maybe_print_script'], 10, 0);
add_action('amp_print_analytics', [$this, 'print_amp_analytics_tag'], 10, 0);
}
public function maybe_print_script(): void
{
$load_script = apply_filters('koko_analytics_load_tracking_script', true);
if (! $load_script) {
return;
}
if (is_request_excluded() || is_preview()) {
return;
}
echo PHP_EOL . '<!-- Koko Analytics v' . KOKO_ANALYTICS_VERSION . ' - https://www.kokoanalytics.com/ -->' . PHP_EOL;
wp_print_inline_script_tag(file_get_contents(KOKO_ANALYTICS_PLUGIN_DIR . '/assets/js/script.js'));
echo PHP_EOL;
}
/**
* Returns the internal ID of the page or post that is being shown.
*
* If page is not a singular object, the function returns 0 if it is the front page (from Settings)
*/
private function get_post_id(): int
{
if (is_singular()) {
return get_queried_object_id();
}
return 0;
}
private function get_tracker_url(): string
{
// People can create their own endpoint and define it through this constant
if (\defined('KOKO_ANALYTICS_CUSTOM_ENDPOINT') && KOKO_ANALYTICS_CUSTOM_ENDPOINT) {
// custom custom endpoint
return site_url(KOKO_ANALYTICS_CUSTOM_ENDPOINT);
} elseif (using_custom_endpoint()) {
// default custom endpoint
return site_url('/koko-analytics-collect.php');
}
// default URL (which includes WordPress)
return home_url('/');
}
public function print_js_object(): void
{
$settings = get_settings();
$script_config = [
// the URL of the tracking endpoint
'url' => $this->get_tracker_url(),
// root URL of site
'site_url' => get_home_url(),
'post_id' => $this->get_post_id(),
'path' => self::get_request_path(),
// tracking method to use (passed to endpoint)
'method' => $settings['tracking_method'],
// for backwards compatibility with older versions
// some users set this value from other client-side scripts, ie cookie consent banners
// if true, takes priority of the method property defined above
'use_cookie' => $settings['tracking_method'] === 'cookie',
];
$data = 'window.koko_analytics = ' . \json_encode($script_config) . ';';
wp_print_inline_script_tag($data);
}
public function print_amp_analytics_tag(): void
{
$settings = get_settings();
$data = [
'action' => 'koko_analytics_collect',
'm' => $settings['tracking_method'][0],
'po' => $this->get_post_id(),
'pa' => self::get_request_path(),
];
$url = add_query_arg($data, $this->get_tracker_url());
$config = [
'requests' => [
'pageview' => $url,
],
'triggers' => [
'trackPageview' => [
'on' => 'visible',
'request' => 'pageview',
],
],
];
echo '<amp-analytics><script type="application/json">', json_encode($config), '</script></amp-analytics>';
}
}