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