PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.3.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.3.0
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 / TrackingCode.php
matomo / classes / WpMatomo Last commit date
Admin 5 years ago Commands 5 years ago Db 5 years ago Ecommerce 5 years ago Report 6 years ago Site 5 years ago TrackingCode 5 years ago Updater 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 5 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 5 years ago Settings.php 5 years ago Site.php 6 years ago TrackingCode.php 5 years ago Uninstaller.php 6 years ago Updater.php 5 years ago User.php 6 years ago
TrackingCode.php
215 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 $contains_noscript_tag = stripos($code, '<noscript') !== false;
133 if (!$contains_noscript_tag) {
134 echo '<noscript>';
135 }
136 echo $code;
137 if (!$contains_noscript_tag) {
138 echo '</noscript>';
139 }
140 echo "\n";
141 } else {
142 $this->logger->log( 'No noscript code present. Blog ID: ' . get_current_blog_id(), Logger::LEVEL_DEBUG );
143 }
144 }
145
146 /**
147 * Add a campaign parameter to feed permalink
148 *
149 * @param string $permalink
150 * permalink
151 *
152 * @return string permalink extended by campaign parameter
153 */
154 public function add_feed_campaign( $permalink ) {
155 global $post;
156 if ( is_feed() && !empty($post) ) {
157 $this->logger->log( 'Add campaign to feed permalink.' );
158 $sep = ( strpos( $permalink, '?' ) === false ? '?' : '&' );
159 $permalink .= $sep . 'pk_campaign=' . rawurlencode( $this->settings->get_global_option( 'track_feed_campaign' ) ) . '&pk_kwd=' . rawurlencode( $post->post_name );
160 }
161
162 return $permalink;
163 }
164
165 /**
166 * Add tracking pixels to feed content
167 *
168 * @param string $content
169 * post content
170 *
171 * @return string post content extended by tracking pixel
172 */
173 public function add_feed_tracking( $content ) {
174 global $post;
175 if ( is_feed() ) {
176 $this->logger->log( 'Add tracking image to feed entry.' );
177 $site = new Site();
178 $site_id = $site->get_current_matomo_site_id();
179 if ( ! $site_id ) {
180 return false;
181 }
182 $title = the_title( null, null, false );
183 $posturl = get_permalink( $post->ID );
184 $urlref = get_bloginfo( 'rss2_url' );
185
186 $tracker_endpoint = $this->generator->get_tracker_endpoint();
187
188 $tracking_image = $tracker_endpoint . '?idsite=' . $site_id . '&amp;rec=1&amp;url=' . rawurlencode( $posturl ) . '&amp;action_name=' . rawurlencode( $title ) . '&amp;urlref=' . rawurlencode( $urlref );
189 $content .= '<img src="' . $tracking_image . '" style="border:0;width:0;height:0" width="0" height="0" alt="" />';
190 }
191
192 return $content;
193 }
194
195 /**
196 * Forwards the cross domain parameter pk_vid if the URL parameter is set and a user is about to be redirected.
197 * When another website links to WooCommerce with a pk_vid parameter, and WooCommerce redirects the user to another
198 * URL, the pk_vid parameter would get lost and the visitorId would later not be applied by the tracking code
199 * due to the lost pk_vid URL parameter. If the URL parameter is set, we make sure to forward this parameter.
200 *
201 * @param string $location
202 *
203 * @return string location extended by pk_vid URL parameter if the URL parameter is set
204 */
205 public function forward_cross_domain_visitor_id( $location ) {
206 if ( ! empty( $_GET['pk_vid'] )
207 && preg_match( '/^[a-zA-Z0-9]{24,60}$/', $_GET['pk_vid'] ) ) {
208 // currently, the pk_vid parameter is 32 characters long, but it may vary over time.
209 $location = add_query_arg( 'pk_vid', $_GET['pk_vid'], $location );
210 }
211
212 return $location;
213 }
214 }
215