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 |