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
TrackingCode.php
207 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 | use \WpMatomo\TrackingCode\TrackingCodeGenerator; |
| 17 | |
| 18 | class TrackingCode { |
| 19 | /** |
| 20 | * @var Settings |
| 21 | */ |
| 22 | private $settings; |
| 23 | |
| 24 | /** |
| 25 | * @var Logger |
| 26 | */ |
| 27 | private $logger; |
| 28 | |
| 29 | /** |
| 30 | * @var TrackingCodeGenerator |
| 31 | */ |
| 32 | private $generator; |
| 33 | |
| 34 | /** |
| 35 | * @param Settings $settings |
| 36 | */ |
| 37 | public function __construct( $settings ) { |
| 38 | $this->settings = $settings; |
| 39 | $this->logger = new Logger(); |
| 40 | $this->generator = new TrackingCodeGenerator( $this->settings ); |
| 41 | $this->generator->register_hooks(); |
| 42 | } |
| 43 | |
| 44 | public function register_hooks() { |
| 45 | if ( $this->settings->is_tracking_enabled() ) { |
| 46 | if ( $this->settings->is_track_feed() ) { |
| 47 | add_filter( 'the_excerpt_rss', array( $this, 'add_feed_tracking' ) ); |
| 48 | add_filter( 'the_content', array( $this, 'add_feed_tracking' ) ); |
| 49 | } |
| 50 | if ( $this->settings->is_add_feed_campaign() ) { |
| 51 | add_filter( 'post_link', array( $this, 'add_feed_campaign' ) ); |
| 52 | } |
| 53 | if ( $this->settings->is_cross_domain_linking_enabled() ) { |
| 54 | add_filter( 'wp_redirect', array( $this, 'forward_cross_domain_visitor_id' ) ); |
| 55 | } |
| 56 | |
| 57 | $is_admin = is_admin() || !empty($GLOBALS['MATOMO_LOADED_DIRECTLY']); |
| 58 | |
| 59 | if ( ! $is_admin || $this->settings->is_admin_tracking_enabled() ) { |
| 60 | $prefix = 'wp'; |
| 61 | if ( $is_admin ) { |
| 62 | $prefix = 'admin'; |
| 63 | } |
| 64 | |
| 65 | $position = $prefix . '_head'; |
| 66 | if ( $this->settings->get_tracking_code_position() === 'footer' ) { |
| 67 | $position = $prefix . '_footer'; |
| 68 | } |
| 69 | |
| 70 | add_action( $position, array( $this, 'add_javascript_code' ) ); |
| 71 | |
| 72 | if ( $this->settings->is_add_no_script_code() ) { |
| 73 | add_action( $prefix . '_footer', array( $this, 'add_noscript_code' ) ); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Check if user should not be tracked |
| 81 | * |
| 82 | * @return boolean Do not track user? |
| 83 | */ |
| 84 | public function is_hidden_user() { |
| 85 | return current_user_can( Capabilities::KEY_STEALTH ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Echo javascript tracking code |
| 90 | */ |
| 91 | public function add_javascript_code() { |
| 92 | if ( $this->is_hidden_user() ) { |
| 93 | $this->logger->log( 'Do not add tracking code to site (user should not be tracked) Blog ID: ' . get_current_blog_id(), Logger::LEVEL_DEBUG ); |
| 94 | |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | $tracking_code = $this->generator->get_tracking_code(); |
| 99 | |
| 100 | $this->logger->log( 'Add tracking code. Blog ID: ' . get_current_blog_id(), Logger::LEVEL_DEBUG ); |
| 101 | |
| 102 | if ( $this->settings->is_network_enabled() |
| 103 | && 'manually' === $this->settings->get_global_option( 'track_mode' ) ) { |
| 104 | $site = new Site(); |
| 105 | $site_id = $site->get_current_matomo_site_id(); |
| 106 | if ( $site_id ) { |
| 107 | $tracking_code = str_replace( '{MATOMO_API_ENDPOINT}', wp_json_encode( $this->generator->get_tracker_endpoint() ), $tracking_code ); |
| 108 | $tracking_code = str_replace( '{MATOMO_JS_ENDPOINT}', wp_json_encode( $this->generator->get_js_endpoint() ), $tracking_code ); |
| 109 | echo str_replace( '{MATOMO_IDSITE}', $site_id, $tracking_code ); |
| 110 | } else { |
| 111 | echo '<!-- Site not yet synced with Matomo, tracking code will be added later -->'; |
| 112 | } |
| 113 | } else { |
| 114 | echo $tracking_code; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Echo noscript tracking code |
| 120 | */ |
| 121 | public function add_noscript_code() { |
| 122 | if ( $this->is_hidden_user() ) { |
| 123 | $this->logger->log( 'Do not add noscript code to site (user should not be tracked) Blog ID: ' . get_current_blog_id(), Logger::LEVEL_DEBUG ); |
| 124 | |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | $code = $this->generator->get_noscript_code(); |
| 129 | |
| 130 | if ( ! empty( $code ) ) { |
| 131 | $this->logger->log( 'Add noscript code. Blog ID: ' . get_current_blog_id(), Logger::LEVEL_DEBUG ); |
| 132 | echo $code . "\n"; |
| 133 | } else { |
| 134 | $this->logger->log( 'No noscript code present. Blog ID: ' . get_current_blog_id(), Logger::LEVEL_DEBUG ); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Add a campaign parameter to feed permalink |
| 140 | * |
| 141 | * @param string $permalink |
| 142 | * permalink |
| 143 | * |
| 144 | * @return string permalink extended by campaign parameter |
| 145 | */ |
| 146 | public function add_feed_campaign( $permalink ) { |
| 147 | global $post; |
| 148 | if ( is_feed() ) { |
| 149 | $this->logger->log( 'Add campaign to feed permalink.' ); |
| 150 | $sep = ( strpos( $permalink, '?' ) === false ? '?' : '&' ); |
| 151 | $permalink .= $sep . 'pk_campaign=' . rawurlencode( $this->settings->get_global_option( 'track_feed_campaign' ) ) . '&pk_kwd=' . rawurlencode( $post->post_name ); |
| 152 | } |
| 153 | |
| 154 | return $permalink; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Add tracking pixels to feed content |
| 159 | * |
| 160 | * @param string $content |
| 161 | * post content |
| 162 | * |
| 163 | * @return string post content extended by tracking pixel |
| 164 | */ |
| 165 | public function add_feed_tracking( $content ) { |
| 166 | global $post; |
| 167 | if ( is_feed() ) { |
| 168 | $this->logger->log( 'Add tracking image to feed entry.' ); |
| 169 | $site = new Site(); |
| 170 | $site_id = $site->get_current_matomo_site_id(); |
| 171 | if ( ! $site_id ) { |
| 172 | return false; |
| 173 | } |
| 174 | $title = the_title( null, null, false ); |
| 175 | $posturl = get_permalink( $post->ID ); |
| 176 | $urlref = get_bloginfo( 'rss2_url' ); |
| 177 | |
| 178 | $tracker_endpoint = $this->generator->get_tracker_endpoint(); |
| 179 | |
| 180 | $tracking_image = $tracker_endpoint . '?idsite=' . $site_id . '&rec=1&url=' . rawurlencode( $posturl ) . '&action_name=' . rawurlencode( $title ) . '&urlref=' . rawurlencode( $urlref ); |
| 181 | $content .= '<img src="' . $tracking_image . '" style="border:0;width:0;height:0" width="0" height="0" alt="" />'; |
| 182 | } |
| 183 | |
| 184 | return $content; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Forwards the cross domain parameter pk_vid if the URL parameter is set and a user is about to be redirected. |
| 189 | * When another website links to WooCommerce with a pk_vid parameter, and WooCommerce redirects the user to another |
| 190 | * URL, the pk_vid parameter would get lost and the visitorId would later not be applied by the tracking code |
| 191 | * due to the lost pk_vid URL parameter. If the URL parameter is set, we make sure to forward this parameter. |
| 192 | * |
| 193 | * @param string $location |
| 194 | * |
| 195 | * @return string location extended by pk_vid URL parameter if the URL parameter is set |
| 196 | */ |
| 197 | public function forward_cross_domain_visitor_id( $location ) { |
| 198 | if ( ! empty( $_GET['pk_vid'] ) |
| 199 | && preg_match( '/^[a-zA-Z0-9]{24,60}$/', $_GET['pk_vid'] ) ) { |
| 200 | // currently, the pk_vid parameter is 32 characters long, but it may vary over time. |
| 201 | $location = add_query_arg( 'pk_vid', $_GET['pk_vid'], $location ); |
| 202 | } |
| 203 | |
| 204 | return $location; |
| 205 | } |
| 206 | } |
| 207 |