PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.8.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.8.2
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / classes / WpMatomo / Ecommerce / ServerSideVisitorId.php
matomo / classes / WpMatomo / Ecommerce Last commit date
Base.php 10 months ago EasyDigitalDownloads.php 4 years ago MatomoTestEcommerce.php 10 months ago MemberPress.php 3 months ago ServerSideVisitorId.php 1 year ago Woocommerce.php 4 months ago
ServerSideVisitorId.php
110 lines
1 <?php
2 /**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 * @package matomo
8 */
9
10 namespace WpMatomo\Ecommerce;
11
12 use WpMatomo\Logger;
13 use WpMatomo\Settings;
14 use WpMatomo\AjaxTracker;
15
16 class ServerSideVisitorId {
17
18 const VISITOR_ID_SESSION_VAR_NAME = 'matomo-for-wordpress-visitor-id';
19
20 /**
21 * @var Settings
22 */
23 private $settings;
24
25 /**
26 * @var Logger
27 */
28 private $logger;
29
30 public function __construct( Settings $settings, Logger $logger ) {
31 $this->settings = $settings;
32 $this->logger = $logger;
33 }
34
35 public function register_hooks() {
36 if ( did_action( 'woocommerce_init' ) ) {
37 $this->force_server_side_visitor_id();
38 } else {
39 add_action( 'woocommerce_init', [ $this, 'force_server_side_visitor_id' ] );
40 }
41 }
42
43 public function force_server_side_visitor_id() {
44 if ( $this->is_visitor_id_cookie_present() ) {
45 return; // cookie found, no need to force a server side generated one
46 }
47
48 if ( is_admin() ) {
49 return;
50 }
51
52 if ( ! empty( $GLOBALS['MATOMO_LOADED_DIRECTLY'] ) ) {
53 return;
54 }
55
56 $this->logger->log( 'ServerSideVisitorId: visitor ID cookie missing from request' );
57
58 // only initialize the session early for requests that do not have the visitor ID cookie
59 $this->initialize_woocommerce_session_if_needed();
60
61 $visitor_id = WC()->session->get( self::VISITOR_ID_SESSION_VAR_NAME );
62 if ( empty( $visitor_id ) ) {
63 $this->logger->log( 'ServerSideVisitorId: no visitor ID in Woocommerce session, generating a new one' );
64
65 $tracker = new AjaxTracker( $this->settings );
66 $visitor_id = $tracker->setNewVisitorId()->randomVisitorId;
67 WC()->session->set( self::VISITOR_ID_SESSION_VAR_NAME, $visitor_id );
68 }
69
70 add_action(
71 'wp_head',
72 function () use ( $visitor_id ) {
73 $this->logger->log( 'ServerSideVisitorId: forcing use of server side visitor ID' );
74
75 echo '<script>window._paq = window._paq || []; window._paq.push(["setVisitorId", ' . wp_json_encode( $visitor_id ) . ']);</script>';
76 }
77 );
78 }
79
80 /**
81 * Checks if any visitor ID cookie is found for the current request. This means it checks
82 * for any cookie with the visitor ID cookie name prefix (_pk_id.). We don't look for the
83 * full cookie name, since that would require getting the configured cookie domain, which
84 * would add an extra DB query to every page load.
85 *
86 * @return bool
87 */
88 private function is_visitor_id_cookie_present() {
89 if ( empty( $_COOKIE ) || ! is_array( $_COOKIE ) ) {
90 return false;
91 }
92
93 $cookie_prefix = AjaxTracker::FIRST_PARTY_COOKIES_PREFIX . 'id.';
94 foreach ( $_COOKIE as $name => $value ) {
95 if ( strpos( $name, $cookie_prefix ) === 0 ) {
96 return true;
97 }
98 }
99
100 return false;
101 }
102
103 private function initialize_woocommerce_session_if_needed() {
104 WC()->initialize_session();
105 if ( ! WC()->session->has_session() ) {
106 WC()->session->set_customer_session_cookie( true );
107 }
108 }
109 }
110