| 1 |
<?php |
| 2 |
|
| 3 |
namespace WebKinder\GoogleAnalytics; |
| 4 |
|
| 5 |
class Loader |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Returns if the cookie is present |
| 9 |
* this cookie is set in the backend for users that select it. |
| 10 |
* |
| 11 |
* @since 1.2 |
| 12 |
* |
| 13 |
* @return bool |
| 14 |
*/ |
| 15 |
public function render_script() |
| 16 |
{ |
| 17 |
ob_start(); |
| 18 |
?> |
| 19 |
function hasWKGoogleAnalyticsCookie() { |
| 20 |
return (new RegExp('wp_wk_ga_untrack_' + document.location.hostname)).test(document.cookie); |
| 21 |
} |
| 22 |
<?php |
| 23 |
return ob_get_clean(); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Outputs the Google Tag Manager script tag. |
| 28 |
* |
| 29 |
* @since 1.2 |
| 30 |
*/ |
| 31 |
public function google_tag_manager_script() |
| 32 |
{ |
| 33 |
$tag_manager_id = get_option('ga_tag_manager_id'); |
| 34 |
$use_tag_manager = get_option('ga_use_tag_manager'); |
| 35 |
if ($use_tag_manager) { |
| 36 |
ob_start(); |
| 37 |
?> |
| 38 |
if (!hasWKGoogleAnalyticsCookie() && shouldTrack()) { |
| 39 |
//Google Tag Manager |
| 40 |
(function (w, d, s, l, i) { |
| 41 |
w[l] = w[l] || []; |
| 42 |
w[l].push({ |
| 43 |
'gtm.start': |
| 44 |
new Date().getTime(), event: 'gtm.js' |
| 45 |
}); |
| 46 |
var f = d.getElementsByTagName(s)[0], |
| 47 |
j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; |
| 48 |
j.async = true; |
| 49 |
j.src = |
| 50 |
'https://www.googletagmanager.com/gtm.js?id=' + i + dl; |
| 51 |
f.parentNode.insertBefore(j, f); |
| 52 |
})(window, document, 'script', 'dataLayer', '<?php echo $tag_manager_id; ?>'); |
| 53 |
} |
| 54 |
<?php |
| 55 |
} |
| 56 |
|
| 57 |
return apply_filters('wk_google_tag_manager_script', $this->should_track_user().$this->render_script().ob_get_clean(), $tag_manager_id, $use_tag_manager); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Outputs the Google Tag Manager noscript tag. |
| 62 |
* |
| 63 |
* @since 1.6 |
| 64 |
*/ |
| 65 |
public function google_tag_manager_noscript() |
| 66 |
{ |
| 67 |
ob_start(); |
| 68 |
|
| 69 |
$tag_manager_id = get_option('ga_tag_manager_id'); |
| 70 |
$use_tag_manager = get_option('ga_use_tag_manager'); |
| 71 |
if ($use_tag_manager) { |
| 72 |
?> |
| 73 |
<noscript> |
| 74 |
<iframe src="https://www.googletagmanager.com/ns.html?id=<?php echo $tag_manager_id; ?>" height="0" width="0" |
| 75 |
style="display:none;visibility:hidden"></iframe> |
| 76 |
</noscript> |
| 77 |
|
| 78 |
<?php |
| 79 |
} |
| 80 |
echo apply_filters('wk_google_tag_manager_noscript', ob_get_clean(), $tag_manager_id, $use_tag_manager); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Outputs the Google Analytics script. |
| 85 |
* |
| 86 |
* @since 1.2 |
| 87 |
* @see https://developers.google.com/analytics/devguides/collection/analyticsjs/ip-anonymization |
| 88 |
*/ |
| 89 |
public function google_analytics_script() |
| 90 |
{ |
| 91 |
$ga_tracking_code = get_option('ga_tracking_code'); |
| 92 |
$anonymize_ip = (false !== get_option('ga_anonymize_ip')) ? (bool) get_option('ga_anonymize_ip') : true; |
| 93 |
|
| 94 |
ob_start(); |
| 95 |
?> |
| 96 |
if (!hasWKGoogleAnalyticsCookie() && shouldTrack()) { |
| 97 |
//Google Analytics |
| 98 |
window.dataLayer = window.dataLayer || []; |
| 99 |
function gtag(){dataLayer.push(arguments);} |
| 100 |
gtag('js', new Date()); |
| 101 |
<?php |
| 102 |
if ($anonymize_ip) { |
| 103 |
?> |
| 104 |
gtag('config', '<?php echo $ga_tracking_code; ?>', { 'anonymize_ip': true }); |
| 105 |
<?php |
| 106 |
} else { |
| 107 |
?> |
| 108 |
gtag('config', '<?php echo $ga_tracking_code; ?>'); |
| 109 |
<?php |
| 110 |
} |
| 111 |
?> |
| 112 |
} |
| 113 |
<?php |
| 114 |
return apply_filters('wk_google_analytics_script', $this->should_track_user().$this->render_script().ob_get_clean(), $ga_tracking_code, $anonymize_ip); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Registers frontend scripts. |
| 119 |
*/ |
| 120 |
public function register_ga_scripts() |
| 121 |
{ |
| 122 |
// Google Tag Manager script in <head> |
| 123 |
$use_tag_manager = get_option('ga_use_tag_manager'); |
| 124 |
if ($use_tag_manager && apply_filters('wk_render_tag_manager_scripts', true)) { |
| 125 |
global $wp_version; |
| 126 |
if (version_compare($wp_version, '5.1', '>=')) { |
| 127 |
// WordPress version is greater than and equal 5.1 supports inline script without registered dependencies |
| 128 |
wp_register_script('wk-tag-manager-script', ''); |
| 129 |
wp_enqueue_script('wk-tag-manager-script'); |
| 130 |
wp_add_inline_script('wk-tag-manager-script', $this->google_tag_manager_script()); |
| 131 |
} else { |
| 132 |
add_action('wp_head', function () { |
| 133 |
echo '<script type="text/javascript">'.$this->google_tag_manager_script().'</script>'; |
| 134 |
}); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
// Google Analytics script in <head> |
| 139 |
if (!$use_tag_manager && apply_filters('wk_render_google_analytics_scripts', true)) { |
| 140 |
$GA_TRACKING_CODE = get_option('ga_tracking_code'); |
| 141 |
wp_register_script('wk-analytics-script', 'https://www.googletagmanager.com/gtag/js?id='.$GA_TRACKING_CODE); |
| 142 |
wp_enqueue_script('wk-analytics-script'); |
| 143 |
wp_add_inline_script('wk-analytics-script', $this->google_analytics_script()); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Registers cookie scripts for opt out shortcode. |
| 149 |
*/ |
| 150 |
public function register_public_scripts() |
| 151 |
{ |
| 152 |
// cookie library |
| 153 |
wp_register_script('cookie-js', plugins_url('js/js.cookie.js', WK_GA_PLUGIN_FILE)); |
| 154 |
|
| 155 |
// admin js for cookies |
| 156 |
wp_register_script('wk-ga-admin-js', plugins_url('js/admin-functions.js', WK_GA_PLUGIN_FILE), ['jquery', 'cookie-js']); |
| 157 |
|
| 158 |
// translate JavaScript |
| 159 |
$translation_array = [ |
| 160 |
'UntrackText' => __('Do not track any visits from this device', 'wk-google-analytics'), |
| 161 |
'TrackText' => __('Do not track any visits from this device', 'wk-google-analytics'), |
| 162 |
'TrackHint' => __('As long as you do not activate this option while you are using the incognito mode of your browser, a cookie will be stored on your device. This cookie will have to be renewed after one year.', 'wk-google-analytics'), |
| 163 |
]; |
| 164 |
wp_localize_script('wk-ga-admin-js', 'wk_ga_text_content', $translation_array); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Outputs a function to decide if the user should be tracked or not. |
| 169 |
* |
| 170 |
* @since 2.0.0 |
| 171 |
*/ |
| 172 |
public function should_track_user() |
| 173 |
{ |
| 174 |
ob_start(); |
| 175 |
?> |
| 176 |
function shouldTrack(){ |
| 177 |
var trackLoggedIn = <?php echo get_option('track_logged_in') ? 'true' : 'false'; ?>; |
| 178 |
var loggedIn = <?php echo is_user_logged_in() ? 'true' : 'false'; ?>; |
| 179 |
if(!loggedIn){ |
| 180 |
return true; |
| 181 |
} else if( trackLoggedIn ) { |
| 182 |
return true; |
| 183 |
} |
| 184 |
return false; |
| 185 |
} |
| 186 |
<?php |
| 187 |
return ob_get_clean(); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Loads all the admin scripts for settings page. |
| 192 |
* |
| 193 |
* @since 1.0 |
| 194 |
* @see https://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts |
| 195 |
* @see https://github.com/js-cookie/js-cookie |
| 196 |
* |
| 197 |
* @param mixed $hook |
| 198 |
*/ |
| 199 |
public function load_admin_styles($hook) |
| 200 |
{ |
| 201 |
if ('settings_page_google_analytics' !== $hook) { |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
// admin styles |
| 206 |
wp_enqueue_style('wk-ga-admin-styles', plugins_url('css/admin-styles.css', WK_GA_PLUGIN_FILE)); |
| 207 |
} |
| 208 |
} |
| 209 |
|