Admin
5 years ago
Commands
6 years ago
Db
6 years ago
Ecommerce
6 years ago
Report
6 years ago
Site
5 years ago
TrackingCode
5 years ago
User
5 years ago
views
6 years ago
API.php
5 years ago
Access.php
6 years ago
AjaxTracker.php
5 years ago
Annotations.php
6 years ago
Bootstrap.php
6 years ago
Capabilities.php
6 years ago
Compatibility.php
6 years ago
Email.php
5 years ago
Installer.php
6 years ago
Logger.php
5 years ago
OptOut.php
5 years ago
Paths.php
6 years ago
PrivacyBadge.php
6 years ago
Referral.php
6 years ago
Roles.php
6 years ago
ScheduledTasks.php
6 years ago
Settings.php
5 years ago
Site.php
6 years ago
TrackingCode.php
5 years ago
Uninstaller.php
6 years ago
Updater.php
6 years ago
User.php
6 years ago
AjaxTracker.php
98 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; |
| 11 | |
| 12 | use WpMatomo\Logger; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; // if accessed directly |
| 16 | } |
| 17 | |
| 18 | if ( ! class_exists( '\PiwikTracker' ) ) { |
| 19 | include_once plugin_dir_path( MATOMO_ANALYTICS_FILE ) . 'app/vendor/piwik/piwik-php-tracker/PiwikTracker.php'; |
| 20 | } |
| 21 | |
| 22 | class AjaxTracker extends \PiwikTracker { |
| 23 | private $has_cookie = false; |
| 24 | private $logger; |
| 25 | |
| 26 | public function __construct( Settings $settings ) { |
| 27 | $this->logger = new Logger(); |
| 28 | |
| 29 | $site = new Site(); |
| 30 | $idsite = $site->get_current_matomo_site_id(); |
| 31 | |
| 32 | if ( ! $idsite ) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | $paths = new Paths(); |
| 37 | |
| 38 | if ( $settings->get_global_option( 'track_api_endpoint' ) === 'restapi' ) { |
| 39 | $api_endpoint = $paths->get_tracker_api_rest_api_endpoint(); |
| 40 | } else { |
| 41 | $api_endpoint = $paths->get_tracker_api_url_in_matomo_dir(); |
| 42 | } |
| 43 | |
| 44 | parent::__construct( $idsite, $api_endpoint ); |
| 45 | |
| 46 | // we are using the tracker only in ajax so the referer contains the actual url |
| 47 | $this->urlReferrer = false; |
| 48 | $this->pageUrl = ! empty( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : false; |
| 49 | |
| 50 | $cookie_domain = $settings->get_tracking_cookie_domain(); |
| 51 | $this->enableCookies( $cookie_domain ); |
| 52 | |
| 53 | if ( $this->loadVisitorIdCookie() ) { |
| 54 | if ( ! empty( $this->cookieVisitorId ) ) { |
| 55 | $this->has_cookie = true; |
| 56 | $this->setVisitorId( $this->cookieVisitorId ); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | protected function setCookie( $cookieName, $cookieValue, $cookieTTL ) { |
| 62 | if ( ! $this->has_cookie ) { |
| 63 | // we only set / overwrite cookies if it is a visitor that has eg no JS enabled or ad blocker enabled etc. |
| 64 | // this way we will track all cart updates and orders into the same visitor on following requests. |
| 65 | // If we recognized the visitor before via cookie we want in our case to make sure to not overwrite |
| 66 | // any cookie |
| 67 | parent::setCookie( $cookieName, $cookieValue, $cookieTTL ); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | protected function sendRequest( $url, $method = 'GET', $data = null, $force = false ) { |
| 72 | if ( ! $this->idSite ) { |
| 73 | $this->logger->log('ecommerce tracking could not find idSite, cannot send request'); |
| 74 | return; // not installed or synced yet |
| 75 | } |
| 76 | $args = array( |
| 77 | 'method' => $method, |
| 78 | ); |
| 79 | if ( ! empty( $data ) ) { |
| 80 | $args['body'] = $data; |
| 81 | } |
| 82 | |
| 83 | // todo at some point we could think about including `matomo.php` here instead of doing an http request |
| 84 | // however we would need to make sure to set a custom tracker response handler to |
| 85 | // 1) Not send any response no matter what happens |
| 86 | // 2) Never exit at any point |
| 87 | |
| 88 | $response = wp_remote_request( $url, $args ); |
| 89 | |
| 90 | if (is_wp_error($response)) { |
| 91 | $this->logger->log_exception('ajax_tracker', new \Exception($response->get_error_message())); |
| 92 | } |
| 93 | |
| 94 | return $response; |
| 95 | } |
| 96 | |
| 97 | } |
| 98 |