-
Notifications
You must be signed in to change notification settings - Fork 2
/
matomo-tracker.php
160 lines (147 loc) · 5.8 KB
/
matomo-tracker.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
<?php
add_action('wp_footer', 'myPlugin_matomo_tracker');
// track download actions
// used with alpha_downloads: https://github.com/netzgestaltung/alpha-downloads
// add_action('ddownload_save_success_before', 'myPlugin_action_tracker');
/**
* Matomo Tracker implementing class MatomoTracker
* =============================================
* https://github.com/netzgestaltung/wordpress-snippets/blob/master/matomo-tracker.php
* Tracks anonymous pageViews on every visit by HTTP Tracking API
*
* Usage:
* Tracks campaigns with the scheme: https://www.domain.tld/?c=<pk_campaign>(-<pk_source>)(-<pk_medum>)(-<pk_keyword>)(-<pk_content>)
*
* Installation:
* Download: https://github.com/matomo-org/matomo-php-tracker
* save MatomoTracker.php in yourThemes <folderRoot>/includes/matomo-php-tracker/MatomoTracker.php
* Integrate this file into yourThemes functions.php and rename "myPlugin" to your themes name
*
* Configuration:
* Specify $tracker_url, $matomo_site_id and $matomo_user_token
*
*
* License: GNU General Public License v2.0
*/
function myPlugin_get_matomo_tracker(){
// Config
// Matomo base URL, for example http://example.org/matomo/ Must be set
$tracker_url = '';
// Specify the site ID to track
$tracker_site_id = 1;
// Specify an API token with at least Write permission, so the Visitor IP address can be recorded
// Learn more about token_auth: https://matomo.org/faq/general/faq_114/
$tracker_user_token = '';
include_once(get_template_directory() . '/includes/matomo-php-tracker/MatomoTracker.php');
MatomoTracker::$URL = $tracker_url;
$matomoTracker = new MatomoTracker($tracker_site_id);
// Specify an API token with at least Write permission, so the Visitor IP address can be recorded
// Learn more about token_auth: https://matomo.org/faq/general/faq_114/
$matomoTracker->setTokenAuth($tracker_user_token);
// You can manually set the visitor details (resolution, time, plugins, etc.)
// See all other ->set* functions available in the MatomoTracker.php file
// $matomoTracker->setResolution(1600, 1400);
// only track anonymous data!
$matomoTracker->setIp('0.0.0.0');
$matomoTracker->deleteCookies();
$matomoTracker->disableCookieSupport();
return $matomoTracker;
}
function myPlugin_action_tracker($download_id){
$matomoTracker = myPlugin_get_matomo_tracker();
// requires "alpha-downloads"
$download_url = get_post_meta($download_id, '_alpha_file_url', true);
$matomoTracker->doTrackAction($download_url, 'download');
}
function myPlugin_matomo_tracker(){
// exclude json calls
if ( isset($_POST['json']) || isset($_GET['json']) ) {
return;
}
// page url
$site_url = '';
$schema = 'https://';
if (empty($_SERVER['HTTPS']) or $_SERVER['HTTPS'] === 'off') {
$schema = 'http://';
}
$uri_parts = explode('?', $_SERVER['REQUEST_URI'], 2);
$site_url .= $schema . $_SERVER['SERVER_NAME'] . $uri_parts[0];
$is_referer = isset($_SERVER['HTTP_REFERER']) ? ( $site_url === $_SERVER['HTTP_REFERER'] ) : false;
// exclude:
// - site_url ist referer
// - admin pages
// - is outside main query
// - is wp doing ajax
// - bot user agents by regex
if ( !$is_referer && !is_admin() && is_main_query() && !wp_doing_ajax() && !preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT']) ) {
$matomoTracker = myPlugin_get_matomo_tracker();
// page title
$page_title = wp_get_document_title();
if ( is_404() ) {
$page_title = '404 not found, Look for 404 Data at Custom Variables';
$matomoTracker->setCustomVariable(1, '404', $site_url, 'page');
$site_url = $schema . $_SERVER['SERVER_NAME'] . '/404';
}
// Campaign Tracking
if ( isset($_GET['c']) ) {
// Matomo related campain query params
$campaign_parts = array(
'pk_campaign',
'pk_source',
'pk_medium',
'pk_kwd',
'pk_content',
);
// get array from query string, delimited by "-"
$campaign_params = explode('-', $_GET['c'], 5);
// how much params are given?
$campaign_param_length = count($campaign_params);
// reduce the parts set to the number of given params
$campaign_parts = array_slice($campaign_parts, 0, $campaign_param_length, true);
// put params and parts together
$campaign = array_combine($campaign_parts, $campaign_params);
// create new querystring
if ( count($campaign) > 0 ) { // if we have campain params
$site_url .= '?' . http_build_query($campaign);
}
}
// Event tracking
// ?mtb={category}-{action}-{name}
// we map short URL IDs to readable Values
// asking for spezific values is very secure but hard to maintain
if ( isset($_GET['mtb']) ) {
$mtb_params = explode('-', $_GET['mtb'], 3);
$mtb_value = false;
if ( is_404() ) {
$mtb_value = true;
}
// Homepage mapping
if ( $mtb_params[0] === 'h' ) {
$mtb_params[0] = 'Home';
}
// Type mapping
if ( $mtb_params[1] === 'b' ) {
$mtb_params[1] = 'Button';
} else if ( $mtb_params[1] === 'l' ) {
$mtb_params[1] = 'Link';
} else if ( $mtb_params[1] === 't' ) {
$mtb_params[1] = 'Thumbnail';
}
// Content Identifier mapping
if ( $mtb_params[2] === 'my-button1' ) {
$mtb_params[2] = 'My first button';
} else if ( $mtb_params[2] === 'my-link2' ) {
$mtb_params[2] = 'My second link';
}
// track event
$matomoTracker->doTrackEvent($mtb_params[0], $mtb_params[1], $mtb_params[2], $mtb_value);
}
// Set the url of visited page that we send to matomo
$matomoTracker->setUrl($site_url);
// Sends Tracker request via http
$matomoTracker->doTrackPageView($page_title);
// You can also track Goal conversions
// $matomoTracker->doTrackGoal($idGoal = 1, $revenue = 42);
}
}
?>