Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules/
/coverage-report/
/.idea
76 changes: 46 additions & 30 deletions Classes/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,6 @@
class Loader
{

/**
* Returns whether the current request should be tracked or not
*
* @since 1.2
* @return boolean
*
*/
function should_track_visit()
{
return (!is_user_logged_in() || get_option('track_logged_in'));
}


/**
* Returns if the cookie is present
*
Expand All @@ -29,13 +16,36 @@ function render_script()
{
ob_start();
?>
<script>
function hasWKGoogleAnalyticsCookie() {
return (new RegExp('wp_wk_ga_untrack_' + document.location.hostname)).test(document.cookie);
}
</script>
<?php
return ob_get_clean();
}

/**
* Outputs a js function that allows a cached page to check if the user should be tracked
*/
public function output_should_track_js_function(){


?>
<script>
function shouldTrack(){
var trackLoggedIn = <?php echo (get_option('track_logged_in') ? get_option('track_logged_in') : 'false'); ?>;
var loggedIn = document.cookie.indexOf("wk-ga-logged-in") !== -1;
if(!loggedIn){
return true;
} else if( trackLoggedIn ) {
return true;
}
return false;
}
</script>
<?php
}

/**
* Outputs the Google Tag Manager script tag if necessary
Expand All @@ -46,11 +56,13 @@ function hasWKGoogleAnalyticsCookie() {
function google_tag_manager_script()
{
ob_start();
if ($this->should_track_visit() && get_option('ga_use_tag_manager')) {
if (get_option('ga_use_tag_manager')) {
$TAG_MANAGER_ID = get_option('ga_tag_manager_id');

$this->output_should_track_js_function();
?>
if (!hasWKGoogleAnalyticsCookie()) {
<script>
if (!hasWKGoogleAnalyticsCookie() && shouldTrack()) {
//Google Tag Manager
(function (w, d, s, l, i) {
w[l] = w[l] || [];
Expand All @@ -66,6 +78,7 @@ function google_tag_manager_script()
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', '<?php echo $TAG_MANAGER_ID; ?>');
}
</script>
<?php
}
return ob_get_clean();
Expand All @@ -81,7 +94,7 @@ function google_tag_manager_script()
function google_tag_manager_noscript()
{
ob_start();
if ($this->should_track_visit() && get_option('ga_use_tag_manager')) {
if (get_option('ga_use_tag_manager')) {
$TAG_MANAGER_ID = get_option('ga_tag_manager_id');
?>
<noscript>
Expand All @@ -105,12 +118,13 @@ function google_tag_manager_noscript()
function google_analytics_script()
{
ob_start();
if ($this->should_track_visit() && !get_option('ga_use_tag_manager')) {
if (!get_option('ga_use_tag_manager')) {
$GA_TRACKING_CODE = get_option('ga_tracking_code');
$ANONYMIZE_IP = (get_option('ga_anonymize_ip') !== false) ? (boolean)get_option('ga_anonymize_ip') : true;
$this->output_should_track_js_function();
?>

if (!hasWKGoogleAnalyticsCookie()) {
<script>
if (!hasWKGoogleAnalyticsCookie() && shouldTrack()) {
//Google Analytics
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
Expand All @@ -135,7 +149,7 @@ function google_analytics_script()

ga('send', 'pageview');
}

</script>
<?php
}
return ob_get_clean();
Expand All @@ -147,20 +161,13 @@ function google_analytics_script()
function register_ga_scripts()
{
//cookie function
wp_register_script('wk-cookie-check', '');
wp_enqueue_script('wk-cookie-check');
wp_add_inline_script('wk-cookie-check', $this->render_script());
echo $this->render_script();

//Google Analytics script in <head>
wp_register_script('wk-tag-manager-script', '');
wp_enqueue_script('wk-tag-manager-script');
wp_add_inline_script('wk-tag-manager-script', $this->google_tag_manager_script());
echo $this->google_tag_manager_script();

//Google Analytics script in <head>
wp_register_script('wk-analytics-script', '');
wp_enqueue_script('wk-analytics-script');
wp_add_inline_script('wk-analytics-script', $this->google_analytics_script());

echo $this->google_analytics_script();
}

/**
Expand Down Expand Up @@ -202,4 +209,13 @@ function load_admin_styles($hook)

}

/**
* Set a logged in cookie that is available to cached pages
* Do not use this cookie for security checks!
*
*/
function set_logged_in_cookie(){
setcookie('wk-ga-logged-in', is_user_logged_in(), time() + 31556926, '/');
}

}
7 changes: 6 additions & 1 deletion Classes/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Plugin
public function run()
{



//i18n
add_action('plugins_loaded', array($this, 'load_textdomain'));

Expand All @@ -26,12 +28,15 @@ public function run()
include_once 'Loader.php';
$this->loader = new Loader();

// set logged in cookie after wp is initialized and auth has happened
add_action( 'init', array($this->loader,'set_logged_in_cookie'));

//cookie handling
add_action('admin_enqueue_scripts', array($this->loader, 'load_admin_styles'));
add_action('wp_enqueue_scripts', array($this->loader, 'register_public_scripts'));
add_action('admin_enqueue_scripts', array($this->loader, 'register_public_scripts'));

add_action('wp_enqueue_scripts', array($this->loader, 'register_ga_scripts'));
add_action('wp_head', array($this->loader, 'register_ga_scripts'));

//Google Tag Manager noscript footer
add_action('wp_footer', array($this->loader, 'google_tag_manager_noscript'));
Expand Down
127 changes: 0 additions & 127 deletions bin/install-wp-tests.sh

This file was deleted.

49 changes: 0 additions & 49 deletions circle.yml

This file was deleted.

30 changes: 0 additions & 30 deletions phpunit.xml.dist

This file was deleted.

Loading