metaboxes
5 months ago
settings
5 months ago
admin.php
5 months ago
advanced.php
6 months ago
ajax.php
5 months ago
columns.php
9 months ago
generatesitemap.php
5 months ago
googleanalytics.php
9 months ago
imageseo.php
8 months ago
import.php
5 months ago
install.php
8 months ago
instantindexing.php
5 months ago
primarycategory.php
9 months ago
socialmetas.php
5 months ago
tableofcontent.php
1 year ago
titlesmetas.php
5 months ago
googleanalytics.php
724 lines
| 1 | <?php |
| 2 | /* |
| 3 | * SITESEO |
| 4 | * https://siteseo.io |
| 5 | * (c) SiteSEO Team |
| 6 | */ |
| 7 | |
| 8 | namespace SiteSEO; |
| 9 | |
| 10 | if(!defined('ABSPATH')){ |
| 11 | die('HACKING ATTEMPT!'); |
| 12 | } |
| 13 | |
| 14 | class GoogleAnalytics{ |
| 15 | |
| 16 | static function ga_render(){ |
| 17 | global $siteseo; |
| 18 | |
| 19 | if(empty($siteseo->setting_enabled['toggle-google-analytics'])){ |
| 20 | return; |
| 21 | } |
| 22 | |
| 23 | if(!empty($siteseo->analaytics_settings['google_analytics_clarity_enable']) && !empty($siteseo->analaytics_settings['google_analytics_clarity_project_id'])){ |
| 24 | add_action('wp_head', '\SiteSEO\GoogleAnalytics::microsoft_clarity'); |
| 25 | } |
| 26 | |
| 27 | if(!empty($siteseo->analaytics_settings['google_analytics_enable']) && !empty($siteseo->analaytics_settings['google_analytics_ga4'])){ |
| 28 | add_action('wp_head', '\SiteSEO\GoogleAnalytics::ga_tracking_code'); |
| 29 | } |
| 30 | |
| 31 | if(!empty($siteseo->analaytics_settings['google_analytics_enable'])){ |
| 32 | add_action('wp_footer', '\SiteSEO\GoogleAnalytics::tracking'); |
| 33 | } |
| 34 | |
| 35 | if(!empty($siteseo->analaytics_settings['google_analytics_link_tracking_enable'])){ |
| 36 | add_action('wp_footer', '\SiteSEO\GoogleAnalytics::add_tracking_script'); |
| 37 | } |
| 38 | |
| 39 | if(!empty($siteseo->analaytics_settings['google_analytics_matomo_enable']) && !empty($siteseo->analaytics_settings['google_analytics_matomo_site_id'])){ |
| 40 | add_action('wp_footer', '\SiteSEO\GoogleAnalytics::matomo_tracking_code'); |
| 41 | } |
| 42 | |
| 43 | if(!empty($siteseo->analaytics_settings['google_analytics_other_tracking'])){ |
| 44 | add_action('wp_head', '\SiteSEO\GoogleAnalytics::add_custom_head_script'); |
| 45 | } |
| 46 | |
| 47 | if(!empty($siteseo->analaytics_settings['google_analytics_other_tracking_body'])){ |
| 48 | add_action('wp_body_open', '\SiteSEO\GoogleAnalytics::add_custom_body_script'); |
| 49 | } |
| 50 | |
| 51 | if(!empty($siteseo->analaytics_settings['google_analytics_other_tracking_footer'])){ |
| 52 | add_action('wp_footer', '\SiteSEO\GoogleAnalytics::add_custom_footer_script'); |
| 53 | } |
| 54 | |
| 55 | if(!empty($siteseo->analaytics_settings['google_analytics_opt_out_edit_choice'])){ |
| 56 | $load_cookies_bar = $siteseo->analaytics_settings['google_analytics_hook']; |
| 57 | add_action($load_cookies_bar, '\SiteSEO\GoogleAnalytics::render_cookie_bar'); |
| 58 | } |
| 59 | |
| 60 | } |
| 61 | |
| 62 | static function render_cookie_bar(){ |
| 63 | global $siteseo; |
| 64 | |
| 65 | if(empty($siteseo->setting_enabled['toggle-google-analytics']) || empty($siteseo->analaytics_settings['google_analytics_disable'])){ |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | // Plugin edit pages on which we should not show the cookie notice. |
| 70 | $page_builders = ['fl_builder', 'elementor-preview', 'ct_builder', 'vc_editable', 'brizy_edit', 'tve', 'pagelayer-live']; |
| 71 | |
| 72 | foreach($page_builders as $builder){ |
| 73 | if(isset($_GET[$builder])){ |
| 74 | return; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // load setting |
| 79 | $auto_accept_cookies = !empty($siteseo->analaytics_settings['google_analytics_half_disable']) ? $siteseo->analaytics_settings['google_analytics_half_disable'] : ''; |
| 80 | $cookies_msg = !empty($siteseo->analaytics_settings['google_analytics_opt_out_msg']) ? $siteseo->analaytics_settings['google_analytics_opt_out_msg'] : 'By visiting our site, you agree to our privacy policy regarding cookies, tracking statistics, etc.'; |
| 81 | $accept_btn_msg = !empty($siteseo->analaytics_settings['google_analytics_opt_out_msg_ok']) ? $siteseo->analaytics_settings['google_analytics_opt_out_msg_ok'] : 'Accept'; |
| 82 | $close_btn_msg = !empty($siteseo->analaytics_settings['google_analytics_opt_out_msg_close']) ? $siteseo->analaytics_settings['google_analytics_opt_out_msg_close'] : 'X'; |
| 83 | $edit_btn_msg = !empty($siteseo->analaytics_settings['google_analytics_opt_out_msg_edit']) ? $siteseo->analaytics_settings['google_analytics_opt_out_msg_edit'] : 'Manage cookies'; |
| 84 | $cookies_expir = !empty($siteseo->analaytics_settings['google_analytics_cb_exp_date']) ? $siteseo->analaytics_settings['google_analytics_cb_exp_date'] : ''; |
| 85 | $bar_postion = !empty($siteseo->analaytics_settings['google_analytics_cb_pos']) ? $siteseo->analaytics_settings['google_analytics_cb_pos'] : '' ; |
| 86 | $bar_width = !empty($siteseo->analaytics_settings['google_analytics_cb_width']) ? $siteseo->analaytics_settings['google_analytics_cb_width'] : ''; |
| 87 | $display_backrop = !empty($siteseo->analaytics_settings['google_analytics_cb_backdrop']) ? true : ''; |
| 88 | |
| 89 | // colors load |
| 90 | $backdrop_bg = !empty($siteseo->analaytics_settings['google_analytics_cb_backdrop_bg']) ? $siteseo->analaytics_settings['google_analytics_cb_backdrop_bg'] : ''; |
| 91 | $cookiebar_bg = !empty($siteseo->analaytics_settings['google_analytics_cb_bg']) ? $siteseo->analaytics_settings['google_analytics_cb_bg'] : '#ffffff'; |
| 92 | $cookiebar_bg_txt = !empty($siteseo->analaytics_settings['google_analytics_cb_txt_col']) ? $siteseo->analaytics_settings['google_analytics_cb_txt_col'] : '#000000'; |
| 93 | $cookiesbar_bg_lk = !empty($siteseo->analaytics_settings['google_analytics_cb_lk_col']) ? $siteseo->analaytics_settings['google_analytics_cb_lk_col'] : '#0073aa'; |
| 94 | $primary_btn_bg = !empty($siteseo->analaytics_settings['google_analytics_cb_btn_bg']) ? $siteseo->analaytics_settings['google_analytics_cb_btn_bg'] : '#0073aa'; |
| 95 | $primary_btn_bg_hov = !empty($siteseo->analaytics_settings['google_analytics_cb_btn_bg']) ? $siteseo->analaytics_settings['google_analytics_cb_btn_bg'] : '#ffffff'; |
| 96 | $primary_btn_txt = !empty($siteseo->analaytics_settings['google_analytics_cb_btn_col']) ? $siteseo->analaytics_settings['google_analytics_cb_btn_col'] : '#005177'; |
| 97 | $primary_btn_txt_hov = !empty($siteseo->analaytics_settings['google_analytics_cb_btn_col']) ? $siteseo->analaytics_settings['google_analytics_cb_btn_col'] : '#ffffff'; |
| 98 | $sec_btn_bg = !empty($siteseo->analaytics_settings['google_analytics_cb_btn_sec_bg']) ? $siteseo->analaytics_settings['google_analytics_cb_btn_sec_bg'] : '#cccccc'; |
| 99 | $sec_btn_bg_txt = !empty($siteseo->analaytics_settings['google_analytics_cb_btn_sec_col']) ? $siteseo->analaytics_settings['google_analytics_cb_btn_sec_col'] : '#000000'; |
| 100 | $sec_btn_bg_hov = !empty($siteseo->analaytics_settings['google_analytics_cb_btn_sec_bg_hov']) ? $siteseo->analaytics_settings['google_analytics_cb_btn_sec_bg_hov'] : '#aaaaaa'; |
| 101 | $sec_btn_txt_hov = !empty($siteseo->analaytics_settings['google_analytics_cb_btn_sec_col_hov']) ? $siteseo->analaytics_settings['google_analytics_cb_btn_sec_col_hov'] : '#000000'; |
| 102 | |
| 103 | //position |
| 104 | $position_class = ''; |
| 105 | $backdrop = false; |
| 106 | switch(strtolower($bar_postion)){ |
| 107 | case 'middle': |
| 108 | $position_class = 'siteseo-cookie-bar-middle'; |
| 109 | $backdrop = true; |
| 110 | break; |
| 111 | case 'top': |
| 112 | $position_class = 'siteseo-cookie-bar-top'; |
| 113 | $backdrop = false; |
| 114 | break; |
| 115 | default: |
| 116 | $position_class = 'siteseo-cookie-bar-bottom'; |
| 117 | $backdrop = false; |
| 118 | } |
| 119 | |
| 120 | /* Translators: %s is the background color */ |
| 121 | $bar_styles = sprintf( |
| 122 | 'background-color: %s; color: %s;', |
| 123 | esc_attr($cookiebar_bg), |
| 124 | esc_attr($cookiebar_bg_txt) |
| 125 | ); |
| 126 | |
| 127 | |
| 128 | $backdrop_html = ''; |
| 129 | if(!empty($display_backrop) && $backdrop){ |
| 130 | $backdrop_html = '<div id="siteseo-cookie-bar-backdrop" style="background-color:'.esc_attr($backdrop_bg).'" class="siteseo-cookie-bar-backdrop"></div>'; |
| 131 | } |
| 132 | |
| 133 | $css = '<style> |
| 134 | #siteseo-cookie-bar-accept{ |
| 135 | --primary-btn-bg: '.esc_attr($primary_btn_bg).'; |
| 136 | --primary-btn-text: '.esc_attr($primary_btn_txt).'; |
| 137 | --primary-btn-hover-bg: '.esc_attr($primary_btn_bg_hov).'; |
| 138 | --primary-btn-hover-text: '.esc_attr($primary_btn_txt_hov).'; |
| 139 | } |
| 140 | #siteseo-cookie-bar-close{ |
| 141 | --secondary-btn-bg: '.esc_attr($sec_btn_bg).'; |
| 142 | --secondary-btn-text: '.esc_attr($sec_btn_bg_txt).'; |
| 143 | --secondary-btn-hover-bg: '.esc_attr($sec_btn_bg_hov).'; |
| 144 | --secondary-btn-hover-text: '.esc_attr($sec_btn_txt_hov).'; |
| 145 | } |
| 146 | </style>'; |
| 147 | |
| 148 | $html = $backdrop_html; |
| 149 | $html .= '<div id="siteseo-cookie-bar" class="siteseo-cookie-bar '.esc_attr($position_class).'" style="'.esc_attr($bar_styles).'" data-half-disable="'.esc_attr($auto_accept_cookies).'"> |
| 150 | <div class="siteseo-cookie-bar-content"> |
| 151 | <span>'.wp_kses_post($cookies_msg).'</span> |
| 152 | <div class="siteseo-cookie-bar-buttons"> |
| 153 | <button id="siteseo-cookie-bar-accept" class="siteseo-cookie-bar-button siteseo-cookie-bar-primary-btn" style="background-color: '.esc_attr($primary_btn_bg).'; color: '.esc_attr($primary_btn_txt).'"> |
| 154 | '.esc_html($accept_btn_msg).' |
| 155 | </button> |
| 156 | <button id="siteseo-cookie-bar-close" class="siteseo-cookie-bar-button siteseo-cookie-bar-secondary-btn" style="background-color: '.esc_attr($sec_btn_bg).'; color: '.esc_attr($sec_btn_bg_txt).'"> |
| 157 | '.esc_html($close_btn_msg).' |
| 158 | </button> |
| 159 | </div> |
| 160 | </div> |
| 161 | </div> |
| 162 | <button id="siteseo-cookie-bar-manage-btn" class="siteseo-cookie-bar-button siteseo-cookie-bar-primary-btn" style="background-color: '.$primary_btn_bg.'; color: '.esc_attr($primary_btn_txt).'"> |
| 163 | '.esc_html($edit_btn_msg).' |
| 164 | </button>'; |
| 165 | |
| 166 | echo wp_kses_post($html); |
| 167 | } |
| 168 | |
| 169 | static function update_src_tag($tag, $handle, $src){ |
| 170 | global $siteseo; |
| 171 | |
| 172 | $tracking_handles = [ |
| 173 | 'siteseo-gtag', |
| 174 | 'siteseo-matomo-tracking', |
| 175 | 'siteseo-microsoft-clarity', |
| 176 | 'siteseo-microsoft-clarity-js-after', |
| 177 | 'siteseo-ga-tracking' |
| 178 | ]; |
| 179 | |
| 180 | if(!in_array($handle, $tracking_handles)){ |
| 181 | return $tag; |
| 182 | } |
| 183 | |
| 184 | if(!empty($siteseo->analaytics_settings['google_analytics_disable']) && !isset($_COOKIE['siteseo-user-consent-accept']) && !isset($_COOKIE['siteseo-user-consent-close'])){ |
| 185 | $tag = str_replace(' src=', ' data-src-siteseo=', $tag); |
| 186 | } |
| 187 | |
| 188 | return $tag; |
| 189 | } |
| 190 | |
| 191 | static function process_script_src($scripts){ |
| 192 | global $siteseo; |
| 193 | |
| 194 | if(!empty($siteseo->analaytics_settings['google_analytics_disable']) && !isset($_COOKIE['siteseo-user-consent-accept']) && !isset($_COOKIE['siteseo-user-consent-close'])){ |
| 195 | $scripts = preg_replace('/(<script[^>]*) src=(["\'])(.*?)\\2/i', '$1 data-src-siteseo=$2$3$2', $scripts); |
| 196 | } |
| 197 | |
| 198 | return $scripts; |
| 199 | } |
| 200 | |
| 201 | static function add_custom_head_script(){ |
| 202 | global $siteseo; |
| 203 | |
| 204 | $scripts = $siteseo->analaytics_settings['google_analytics_other_tracking']; |
| 205 | |
| 206 | $scripts = self::process_script_src($scripts); |
| 207 | |
| 208 | echo wp_kses($scripts, [ |
| 209 | 'script' => [ |
| 210 | 'async' => [], |
| 211 | 'src' => [], |
| 212 | 'data-src-siteseo' => [], |
| 213 | 'type' => [] |
| 214 | ] |
| 215 | ]); |
| 216 | } |
| 217 | |
| 218 | static function add_custom_body_script(){ |
| 219 | global $siteseo; |
| 220 | |
| 221 | $scripts = $siteseo->analaytics_settings['google_analytics_other_tracking_body']; |
| 222 | |
| 223 | $scripts = self::process_script_src($scripts); |
| 224 | |
| 225 | echo wp_kses($scripts, [ |
| 226 | 'script' => [ |
| 227 | 'async' => [], |
| 228 | 'src' => [], |
| 229 | 'data-src-siteseo' => [], |
| 230 | 'type' => [] |
| 231 | ] |
| 232 | ]); |
| 233 | } |
| 234 | |
| 235 | static function add_custom_footer_script(){ |
| 236 | global $siteseo; |
| 237 | |
| 238 | $scripts = $siteseo->analaytics_settings['google_analytics_other_tracking_footer']; |
| 239 | |
| 240 | $scripts = self::process_script_src($scripts); |
| 241 | |
| 242 | echo wp_kses($scripts, [ |
| 243 | 'script' => [ |
| 244 | 'async' => [], |
| 245 | 'src' => [], |
| 246 | 'data-src-siteseo' => [], |
| 247 | 'type' => [] |
| 248 | ] |
| 249 | ]); |
| 250 | } |
| 251 | |
| 252 | static function exclude_user_tracking(){ |
| 253 | global $siteseo; |
| 254 | |
| 255 | if(!is_user_logged_in()){ |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | $current_user = wp_get_current_user(); |
| 260 | if(!$current_user){ |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | $excluded_roles = isset($siteseo->analaytics_settings['google_analytics_roles']) ? $siteseo->analaytics_settings['google_analytics_roles'] : []; |
| 265 | |
| 266 | if(empty($excluded_roles)){ |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | foreach($current_user->roles as $user_role){ |
| 271 | if(isset($excluded_roles[$user_role])){ |
| 272 | return true; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | static function custom_dimensions(){ |
| 280 | global $siteseo; |
| 281 | $dimensions = []; |
| 282 | $settings = $siteseo->analaytics_settings; |
| 283 | |
| 284 | // Track Authors |
| 285 | if(!empty($settings['track_authors']) && $settings['track_authors'] !== 'none'){ |
| 286 | if(is_singular()){ |
| 287 | $author_id = get_post_field('post_author', get_the_ID()); |
| 288 | $author_name = get_the_author_meta('display_name', $author_id); |
| 289 | $dimensions[$settings['track_authors']] = $author_name; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // Track Categories |
| 294 | if(!empty($settings['track_categories']) && $settings['track_categories'] !== 'none'){ |
| 295 | if(is_singular()){ |
| 296 | $categories = get_the_category(); |
| 297 | if(!empty($categories)){ |
| 298 | $category_names = array_map(function($cat){ |
| 299 | return $cat->name; |
| 300 | }, $categories); |
| 301 | $dimensions[$settings['track_categories']] = implode(', ', $category_names); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // Track Tags |
| 307 | if(!empty($settings['track_tags']) && $settings['track_tags'] !== 'none'){ |
| 308 | if(is_singular()){ |
| 309 | $tags = get_the_tags(); |
| 310 | if(!empty($tags)){ |
| 311 | $tag_names = array_map(function($tag){ |
| 312 | return $tag->name; |
| 313 | }, $tags); |
| 314 | $dimensions[$settings['track_tags']] = implode(', ', $tag_names); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // Track Post Types |
| 320 | if(!empty($settings['track_post_types']) && $settings['track_post_types'] !== 'none'){ |
| 321 | if(is_singular()){ |
| 322 | $dimensions[$settings['track_post_types']] = get_post_type(); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | // Track Logged In Users |
| 327 | if(!empty($settings['track_user']) && $settings['track_user'] !== 'none'){ |
| 328 | if(is_user_logged_in()){ |
| 329 | $current_user = wp_get_current_user(); |
| 330 | $dimensions[$settings['track_user']] = $current_user->roles[0]; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | return $dimensions; |
| 335 | |
| 336 | } |
| 337 | |
| 338 | static function matomo_tracking_code(){ |
| 339 | global $siteseo; |
| 340 | |
| 341 | if(self::exclude_user_tracking()){ |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | $settings = $siteseo->analaytics_settings; |
| 346 | |
| 347 | $tracking_url = !empty($settings['google_analytics_matomo_id']) ? $settings['google_analytics_matomo_id'] : ''; |
| 348 | $site_id = !empty($settings['google_analytics_matomo_site_id']) ? $settings['google_analytics_matomo_site_id'] : ''; |
| 349 | $cross_domain = !empty($settings['google_analytics_matomo_cross_domain']) ? $settings['google_analytics_matomo_cross_domain'] : ''; |
| 350 | $do_not_track = !empty($settings['google_analytics_matomo_dnt']) ? $settings['google_analytics_matomo_dnt'] : ''; |
| 351 | $disable_cookies = !empty($settings['google_analytics_matomo_no_cookies']) ? $settings['google_analytics_matomo_no_cookies'] : ''; |
| 352 | $disable_heatmaps = !empty($settings['google_analytics_matomo_no_heatmaps']) ? $settings['google_analytics_matomo_no_heatmaps'] : ''; |
| 353 | $track_subdomains = !empty($settings['google_analytics_matomo_subdomains']) ? $settings['google_analytics_matomo_subdomains'] : ''; |
| 354 | $track_js_disabled = !empty($settings['google_analytics_matomo_no_js']) ? $settings['google_analytics_matomo_no_js'] : ''; |
| 355 | |
| 356 | wp_register_script('siteseo-matomo-tracking', false, [], null, [ |
| 357 | 'strategy' => true, |
| 358 | 'in_footer' => true, |
| 359 | ]); |
| 360 | wp_enqueue_script('siteseo-matomo-tracking'); |
| 361 | |
| 362 | add_filter('script_loader_tag', '\SiteSEO\GoogleAnalytics::update_src_tag', 10, 3); |
| 363 | wp_add_inline_script('siteseo-matomo-tracking', "var _paq = _paq || []; |
| 364 | _paq.push(['setSiteId', '".esc_html($site_id)."']); |
| 365 | _paq.push(['setTrackerUrl', '".esc_html($tracking_url)."']); |
| 366 | |
| 367 | if(".esc_html($cross_domain).") _paq.push(['enableCrossDomainLinking']); |
| 368 | if(".esc_html($do_not_track).") _paq.push(['setDoNotTrack', true]); |
| 369 | if(".esc_html($disable_cookies).") _paq.push(['disableCookies']); |
| 370 | if(".esc_html($disable_heatmaps).") _paq.push(['disableAllHeatmaps']); |
| 371 | if(".esc_html($track_subdomains).") _paq.push(['setDocumentTitle', document.domain + '/' + document.title]); |
| 372 | |
| 373 | _paq.push(['trackPageView']); |
| 374 | _paq.push(['enableLinkTracking']); |
| 375 | |
| 376 | (function(){ |
| 377 | var u=\"".esc_html($tracking_url)."\"; |
| 378 | _paq.push(['setTrackerUrl', u + 'matomo.php']); |
| 379 | _paq.push(['setSiteId', '".esc_html($site_id)."']); |
| 380 | var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; |
| 381 | g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); |
| 382 | })();"); |
| 383 | |
| 384 | if($track_js_disabled){ |
| 385 | echo '<noscript><img src="'.esc_url($tracking_url.'/matomo.php?idsite='.$site_id.'&rec=1').'" style="border:0" alt="" /></noscript>'; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | |
| 390 | static function add_tracking_script(){ |
| 391 | global $siteseo; |
| 392 | |
| 393 | if(self::exclude_user_tracking()){ |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | echo '<script> |
| 398 | document.addEventListener("DOMContentLoaded", function() {'; |
| 399 | |
| 400 | if(!empty($siteseo->analaytics_settings['google_analytics_link_tracking_enable'])){ |
| 401 | echo 'document.querySelectorAll("a").forEach(function(link){ |
| 402 | if(link.hostname !== location.hostname){ |
| 403 | link.addEventListener("click", function(){ |
| 404 | gtag("event", "click", { "event_category": "External Link", "event_label": link.href }); |
| 405 | }); |
| 406 | } |
| 407 | });'; |
| 408 | } |
| 409 | |
| 410 | if(!empty($siteseo->analaytics_settings['google_analytics_download_tracking_enable']) && !empty($siteseo->analaytics_settings['google_analytics_download_tracking'])) { |
| 411 | |
| 412 | $fileExtensions = preg_replace('/\s+/', '', $siteseo->analaytics_settings['google_analytics_download_tracking']); |
| 413 | $fileExtensionsPattern = str_replace('|', '|\\.', $fileExtensions); |
| 414 | |
| 415 | echo 'document.querySelectorAll("a[href$=\'.' .esc_js(str_replace('|', '\'], a[href$=\'.', $fileExtensions)) . '\']").forEach(function(link) { |
| 416 | link.addEventListener("click", function() { |
| 417 | gtag("event", "download", { "event_category": "Download", "event_label": link.href }); |
| 418 | }); |
| 419 | });'; |
| 420 | } |
| 421 | |
| 422 | if(!empty($siteseo->analaytics_settings['google_analytics_affiliate_tracking_enable']) && ! empty($siteseo->analaytics_settings['google_analytics_affiliate_tracking'])){ |
| 423 | $keywords = wp_json_encode( explode( ',', $siteseo->analaytics_settings['google_analytics_affiliate_tracking'])); |
| 424 | echo 'const keywords = '.esc_attr($keywords).'; |
| 425 | document.querySelectorAll("a").forEach(function(link){ |
| 426 | keywords.forEach(function(keyword){ |
| 427 | if(link.href.includes(keyword.trim())){ |
| 428 | link.addEventListener("click", function(){ |
| 429 | gtag("event", "click", { "event_category": "Affiliate/Outbound Link", "event_label": link.href }); |
| 430 | }); |
| 431 | } |
| 432 | }); |
| 433 | });'; |
| 434 | } |
| 435 | |
| 436 | if(!empty($siteseo->analaytics_settings['google_analytics_phone_tracking'])){ |
| 437 | echo 'document.querySelectorAll("a[href^=\'tel:\']").forEach(function(link){ |
| 438 | link.addEventListener("click", function() { |
| 439 | gtag("event", "click", { "event_category": "Telephone Link", "event_label": link.href }); |
| 440 | }); |
| 441 | });'; |
| 442 | } |
| 443 | |
| 444 | echo '}); |
| 445 | </script>'; |
| 446 | } |
| 447 | |
| 448 | static function tracking(){ |
| 449 | global $siteseo; |
| 450 | |
| 451 | if(self::exclude_user_tracking()){ |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | $settings = $siteseo->analaytics_settings; |
| 456 | |
| 457 | $ga_id = !empty($settings['google_analytics_optimize']) ? $settings['google_analytics_optimize'] : ''; |
| 458 | $conversion_id = !empty($settings['google_analytics_ads']) ? $settings['google_analytics_ads'] : ''; |
| 459 | $optimize_id = !empty($settings['google_analytics_ads']) ? $settings['google_analytics_ads'] : ''; |
| 460 | $remarketing = !empty($settings['google_analytics_remarketing']) ? $settings['google_analytics_remarketing'] : ''; |
| 461 | $anonymize_ip = !empty($settings['google_analytics_ip_anonymization']) ? $settings['google_analytics_ip_anonymization'] : ''; |
| 462 | $enhanced_link = !empty($settings['google_analytics_link_attribution']) ? $settings['google_analytics_link_attribution'] : ''; |
| 463 | $cross_domain = !empty($settings['google_analytics_cross_domain']) ? $settings['google_analytics_cross_domain'] : ''; |
| 464 | $cross_domain_name = !empty($settings['google_analytics_remarketing']) ? $settings['google_analytics_remarketing'] : ''; |
| 465 | |
| 466 | //custom dimensions |
| 467 | $get_custom_dimensions = self::custom_dimensions(); |
| 468 | |
| 469 | wp_enqueue_script('siteseo-gtag', 'https://www.googletagmanager.com/gtag/js?id=' . esc_attr($ga_id), [], SITESEO_VERSION, [ |
| 470 | 'strategy' => 'async', |
| 471 | ]); |
| 472 | |
| 473 | add_filter('script_loader_tag', '\SiteSEO\GoogleAnalytics::update_src_tag', 10, 3); |
| 474 | |
| 475 | $gtag_config = [ |
| 476 | 'anonymize_ip' => $anonymize_ip ? true : false, |
| 477 | 'link_attribution' => $enhanced_link ? true : false, |
| 478 | ]; |
| 479 | |
| 480 | if($cross_domain && $cross_domain_name){ |
| 481 | $gtag_config['linker'] = [ |
| 482 | 'domains' => [$cross_domain_name] |
| 483 | ]; |
| 484 | } |
| 485 | |
| 486 | // config |
| 487 | foreach($get_custom_dimensions as $dimension => $value){ |
| 488 | $gtag_config[$dimension] = $value; |
| 489 | } |
| 490 | |
| 491 | $inline_script = 'window.dataLayer = window.dataLayer || []; |
| 492 | function gtag(){dataLayer.push(arguments);} |
| 493 | gtag("js", new Date()); |
| 494 | |
| 495 | window.addEventListener("load", function (){ |
| 496 | var links = document.querySelectorAll("a"); |
| 497 | for(let i = 0; i < links.length; i++){ |
| 498 | links[i].addEventListener("click", function(e) { |
| 499 | var n = this.href.includes("' . wp_parse_url(home_url(), PHP_URL_HOST) . '"); |
| 500 | if (n == false) { |
| 501 | gtag("event", "click", {"event_category": "external links","event_label" : this.href}); |
| 502 | } |
| 503 | }); |
| 504 | } |
| 505 | }); |
| 506 | |
| 507 | gtag("config", "'.esc_js($ga_id).'", '.wp_json_encode($gtag_config).');'; |
| 508 | |
| 509 | if($optimize_id){ |
| 510 | $inline_script .= 'gtag("config", "'.esc_js($optimize_id).'");'; |
| 511 | } |
| 512 | |
| 513 | if($conversion_id){ |
| 514 | $inline_script .= 'gtag("config", "'.esc_js($conversion_id).'");'; |
| 515 | } |
| 516 | |
| 517 | if($remarketing){ |
| 518 | $inline_script .= 'gtag("set", "allow_google_signals", true);'; |
| 519 | } |
| 520 | |
| 521 | wp_add_inline_script('siteseo-gtag', $inline_script); |
| 522 | } |
| 523 | |
| 524 | static function microsoft_clarity(){ |
| 525 | global $siteseo; |
| 526 | |
| 527 | if(self::exclude_user_tracking()){ |
| 528 | return; |
| 529 | } |
| 530 | |
| 531 | $project_id = !empty($siteseo->analaytics_settings['google_analytics_clarity_project_id']) ? $siteseo->analaytics_settings['google_analytics_clarity_project_id'] : ''; |
| 532 | |
| 533 | if(empty($project_id)){ |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | wp_register_script('siteseo-microsoft-clarity', '', [], SITESEO_VERSION, true); |
| 538 | |
| 539 | wp_enqueue_script('siteseo-microsoft-clarity'); |
| 540 | |
| 541 | $inline_script = "(function(c,l,a,r,i,t,y){ |
| 542 | c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)}; |
| 543 | t=l.createElement(r);t.async=1;t.src='https://www.clarity.ms/tag/'+i; |
| 544 | y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y); |
| 545 | })(window, document, 'clarity', 'script', '".esc_js($project_id)."');"; |
| 546 | |
| 547 | wp_add_inline_script('siteseo-microsoft-clarity', $inline_script); |
| 548 | |
| 549 | add_filter('script_loader_tag', '\SiteSEO\GoogleAnalytics::update_src_tag', 10, 3); |
| 550 | |
| 551 | } |
| 552 | |
| 553 | static function ga_tracking_code(){ |
| 554 | global $siteseo; |
| 555 | |
| 556 | if(self::exclude_user_tracking()){ |
| 557 | return; |
| 558 | } |
| 559 | |
| 560 | $ga_id = isset($siteseo->analaytics_settings['google_analytics_ga4']) ? esc_attr($siteseo->analaytics_settings['google_analytics_ga4']) : ''; |
| 561 | |
| 562 | if(empty($ga_id)){ |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | wp_enqueue_script('siteseo-ga-tracking', 'https://www.googletagmanager.com/gtag/js?id=' . $ga_id, [], SITESEO_VERSION, true); |
| 567 | |
| 568 | add_filter('script_loader_tag', '\SiteSEO\GoogleAnalytics::add_async_attribute', 10, 2); |
| 569 | |
| 570 | $inline_script = " |
| 571 | window.dataLayer = window.dataLayer || []; |
| 572 | function gtag(){dataLayer.push(arguments);} |
| 573 | gtag('js', new Date()); |
| 574 | gtag('config', '{$ga_id}'); |
| 575 | "; |
| 576 | |
| 577 | wp_add_inline_script('siteseo-ga-tracking', $inline_script); |
| 578 | |
| 579 | } |
| 580 | |
| 581 | static function add_async_attribute($tag, $handle){ |
| 582 | global $siteseo; |
| 583 | if('siteseo-ga-tracking' === $handle){ |
| 584 | |
| 585 | if(!empty($siteseo->analaytics_settings['google_analytics_disable'])){ |
| 586 | if(isset($_COOKIE['siteseo-user-consent-accept']) && $_COOKIE['siteseo-user-consent-accept'] === 'true'){ |
| 587 | return str_replace(' src', ' async src', $tag); |
| 588 | } else{ |
| 589 | return str_replace(' src', ' data-src-siteseo', $tag); |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | return $tag; |
| 595 | } |
| 596 | |
| 597 | /** TODO:: temporary in this file*/ |
| 598 | static function handle_custom_redirect(){ |
| 599 | global $post; |
| 600 | |
| 601 | // post types and taxonomies |
| 602 | $post_types = siteseo_post_types(); |
| 603 | $taxonomies = get_taxonomies(array('public' => true), 'objects'); |
| 604 | |
| 605 | |
| 606 | $is_singular = false; |
| 607 | foreach($post_types as $post_type){ |
| 608 | if(is_singular($post_type->name)){ |
| 609 | $is_singular = true; |
| 610 | break; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | $is_taxonomy = false; |
| 615 | foreach($taxonomies as $taxonomy){ |
| 616 | if(is_tax($taxonomy->name)){ |
| 617 | $is_taxonomy = true; |
| 618 | break; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | if($is_singular || is_404() || $is_taxonomy || is_category() || is_tag()){ |
| 623 | if(is_404()){ |
| 624 | $args = array( |
| 625 | 'post_type' => 'siteseo_404', |
| 626 | 'posts_per_page' => 1, |
| 627 | 'meta_query' => array( |
| 628 | array( |
| 629 | 'key' => '_siteseo_redirections_enabled', |
| 630 | 'value' => 'yes', |
| 631 | 'compare' => '=' |
| 632 | ) |
| 633 | ) |
| 634 | ); |
| 635 | $redirect_posts = get_posts($args); |
| 636 | |
| 637 | if(empty($redirect_posts)){ |
| 638 | return; |
| 639 | } |
| 640 | $post = $redirect_posts[0]; |
| 641 | } |
| 642 | |
| 643 | // Taxonomy archives |
| 644 | if($is_taxonomy || is_category() || is_tag()){ |
| 645 | $term = get_queried_object(); |
| 646 | $term_id = $term->term_id; |
| 647 | |
| 648 | // Check redirection is enabled |
| 649 | $enable_redirect = get_term_meta($term_id, '_siteseo_redirections_enabled', true); |
| 650 | |
| 651 | if(empty($enable_redirect)){ |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | $login_status = get_term_meta($term_id, '_siteseo_redirections_logged_status', true); |
| 656 | $redirect_type = get_term_meta($term_id, '_siteseo_redirections_type', true); |
| 657 | $redirect_url = get_term_meta($term_id, '_siteseo_redirections_value', true); |
| 658 | $param_handling = get_term_meta($term_id, '_siteseo_redirections_param', true); |
| 659 | } |
| 660 | // Singular posts, pages, and products |
| 661 | else{ |
| 662 | $enable_redirect = get_post_meta($post->ID, '_siteseo_redirections_enabled', true); |
| 663 | |
| 664 | if(empty($enable_redirect)){ |
| 665 | return; |
| 666 | } |
| 667 | |
| 668 | $login_status = get_post_meta($post->ID, '_siteseo_redirections_logged_status', true); |
| 669 | $redirect_type = get_post_meta($post->ID, '_siteseo_redirections_type', true); |
| 670 | $redirect_url = get_post_meta($post->ID, '_siteseo_redirections_value', true); |
| 671 | $param_handling = get_post_meta($post->ID, '_siteseo_redirections_param', true); |
| 672 | } |
| 673 | |
| 674 | if($login_status === 'only_logged_in' && !is_user_logged_in()){ |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | if($login_status === 'only_not_logged_in' && is_user_logged_in()){ |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | if(!empty($redirect_url)){ |
| 683 | $final_url = $redirect_url; |
| 684 | |
| 685 | if(is_404() && !empty($_SERVER['QUERY_STRING'])){ |
| 686 | switch($param_handling){ |
| 687 | case 'exact_match': |
| 688 | $current_params = sanitize_text_field(wp_unslash($_SERVER['QUERY_STRING'])); |
| 689 | $redirect_params = wp_parse_url($redirect_url, PHP_URL_QUERY); |
| 690 | if($current_params !== $redirect_params){ |
| 691 | return; |
| 692 | } |
| 693 | break; |
| 694 | |
| 695 | case 'without_param': |
| 696 | $final_url = strtok($redirect_url, '?'); |
| 697 | break; |
| 698 | |
| 699 | case 'with_ignored_param': |
| 700 | $query_string = sanitize_text_field(wp_unslash($_SERVER['QUERY_STRING'])); |
| 701 | $final_url = $redirect_url; |
| 702 | if(!empty($query_string)){ |
| 703 | $final_url .= (strpos($redirect_url, '?') !== false ? '&' : '?') . $query_string; |
| 704 | } |
| 705 | break; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | $status_code = !empty($redirect_type) ? intval($redirect_type) : 301; |
| 710 | |
| 711 | if(in_array($status_code, [410, 451]) && is_404()){ |
| 712 | status_header($status_code); |
| 713 | nocache_headers(); |
| 714 | include(get_query_template('404')); |
| 715 | exit; |
| 716 | } |
| 717 | |
| 718 | wp_redirect($final_url, $status_code); |
| 719 | exit; |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 |