Repository navigation
Expand file tree
/
Copy pathsend-stats.php
More file actions
119 lines (104 loc) · 4.07 KB
/
Copy pathsend-stats.php
File metadata and controls
119 lines (104 loc) · 4.07 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
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'wp_head', 'lap_matomo_http_api_js' );
add_action( 'wp_body_open' , 'lap_matomo_http_api_head' );
function lap_matomo_http_api_js() {
if ( is_admin() ) {
return;
}
$options = get_option( 'lap_matomo_http_api_options' );
if ( current_user_can( 'manage_options' ) && empty( $options['track_admins'] ) ) {
return;
}
if ( empty( $options['enable_js'] ) || $options['enable_js'] !== '1' ) {
return;
}
if (!empty($options['url']) && !empty($options['idsite'])) {
$url = esc_js( trailingslashit( $options['url'] ) );
$idsite = esc_js( $options['idsite'] );
?>
<!-- Matomo -->
<script>
var _paq = window._paq = window._paq || [];
_paq.push(["disableCookies"]);
_paq.push(['enableLinkTracking']);
_paq.push(['enableHeartBeatTimer', 15]);
(function() {
var u="<?php echo $url; ?>";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '<?php echo $idsite; ?>']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Matomo Code -->
<?php
}
}
function lap_matomo_http_api_head() {
if ( is_admin() ) {
return;
}
include_once plugin_dir_path( __FILE__ ) .'MatomoTracker.php';
$options = get_option( 'lap_matomo_http_api_options' );
if ( current_user_can( 'manage_options' ) && empty( $options['track_admins'] ) ) {
return;
}
if (!empty($options['url']) && !empty($options['idsite']) && !empty($options['tokenAuth'])) {
$matomoUrl = $options['url'];
$matomoSiteId = $options['idsite'];
$authToken = $options['tokenAuth'];
$debugging = $options['debugging'];
$page_title = esc_html(wp_get_document_title());
$display_name = '';
if (is_user_logged_in()) {
$user_info = get_userdata(get_current_user_id());
$display_name = $user_info->display_name;
}
$connecting_ip = $_SERVER['REMOTE_ADDR'];
if ( array_key_exists( 'HTTP_X_FORWARDED_FOR', $_SERVER ) ) {
$forwarded_ips = explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] );
$connecting_ip = trim( $forwarded_ips[0] );
} elseif ( array_key_exists( 'HTTP_CF_CONNECTING_IP', $_SERVER ) ) {
$connecting_ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
}
try {
$matomoTracker = new MatomoTracker($matomoSiteId, $matomoUrl);
$matomoTracker->setRequestTimeout(2);
$matomoTracker->setTokenAuth($authToken);
$matomoTracker->setRequestMethodNonBulk('POST');
if (is_user_logged_in()) {
$matomoTracker->setUserId($display_name);
}
$matomoTracker->setIp($connecting_ip);
$response = $matomoTracker->doTrackPageView( $page_title );
// Matomo answers a successful tracking request with a 1x1 GIF.
if (is_string($response) && strpos($response, 'GIF') === 0) {
$status = 'OK - Matomo accepted the page view';
} elseif ($response === '' || $response === false || $response === null) {
$status = 'Failed - empty response from Matomo (check URL, token_auth and site ID)';
error_log('Matomo tracking error: empty response for site ' . $matomoSiteId);
} else {
$status = 'Failed - Matomo replied: ' . wp_strip_all_tags(substr($response, 0, 300));
error_log('Matomo tracking error: ' . substr($response, 0, 300));
}
} catch (Exception $e) {
$status = 'Failed - ' . $e->getMessage();
error_log('Matomo tracking error: ' . $e->getMessage());
}
if (current_user_can('manage_options') && $debugging === '1') {
echo '<details>';
echo '<summary>Debugging</summary>';
echo 'Status: ' . esc_html($status) . '<br>';
echo 'Server: ' . esc_html($_SERVER['REMOTE_ADDR']) . '<br>';
echo 'Visitor IP: ' . esc_html($connecting_ip) . '<br>';
echo 'User: ' . esc_html($display_name) . '<br>';
echo 'URL: ' . esc_html($matomoUrl) . '<br>';
echo 'Site: ' . esc_html($matomoSiteId) . '<br>';
echo 'Page Title: '. esc_html($page_title). '<br>';
echo '</details>';
}
}
}