Skip to content

Commit

Permalink
add setting to exclude pageviews from ip addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Mar 10, 2024
1 parent 674a6cc commit 47cdbad
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/class-admin.php
Expand Up @@ -206,6 +206,7 @@ public function save_settings(): void
check_admin_referer('koko_analytics_save_settings');
$new_settings = $_POST['koko_analytics_settings'];
$settings = get_settings();
$settings['exclude_ip_addresses'] = array_filter(array_map('trim', explode(PHP_EOL, str_replace(',', PHP_EOL, strip_tags($new_settings['exclude_ip_addresses'])))), function($value) { return $value !== ''; });
$settings['exclude_user_roles'] = $new_settings['exclude_user_roles'] ?? array();
$settings['prune_data_after_months'] = abs((int) $new_settings['prune_data_after_months']);
$settings['use_cookie'] = (int) $new_settings['use_cookie'];
Expand Down
8 changes: 8 additions & 0 deletions src/class-script-loader.php
Expand Up @@ -38,6 +38,14 @@ public function maybe_enqueue_script(bool $echo = false)
}
}

// Do not load script if excluded by IP address
if (count($settings['exclude_ip_addresses']) > 0) {
$ip_address = $_SERVER['REMOTE_ADDR'] ?? '';
if ($ip_address !== '' && in_array($ip_address, $settings['exclude_ip_addresses'], true)) {
return;
}
}

// TODO: Handle "term" requests so we track both terms and post types.
add_filter('script_loader_tag', array( $this, 'add_async_attribute' ), 20, 2);

Expand Down
1 change: 1 addition & 0 deletions src/functions.php
Expand Up @@ -154,6 +154,7 @@ function get_settings(): array
$default_settings = array(
'use_cookie' => 1,
'exclude_user_roles' => array(),
'exclude_ip_addresses' => array(),
'prune_data_after_months' => 5 * 12, // 5 years
'default_view' => 'last_28_days',
'is_dashboard_public' => 0,
Expand Down
17 changes: 16 additions & 1 deletion src/views/settings-page.php
Expand Up @@ -42,10 +42,25 @@
?>
</select>
<p class="description">
<?php esc_html_e('Visits and pageviews from users with any of the selected roles will be ignored.', 'koko-analytics'); ?>
<?php esc_html_e('Visits and pageviews from any of the selected user roles will be ignored.', 'koko-analytics'); ?>
<?php esc_html_e('Use CTRL to select multiple options.', 'koko-analytics'); ?>
</p>
</div>

<div class="ka-margin-m">
<label for="ka-exclude-ip-addresses" class="ka-settings--label"><?php esc_html_e('Exclude pageviews from these IP addresses', 'koko-analytics'); ?></label>
<textarea id="ka-exclude-ip-addresses" name="koko_analytics_settings[exclude_ip_addresses]" class="widefat" rows="6"><?php foreach($settings['exclude_ip_addresses'] as $ip) {
echo esc_html($ip) . PHP_EOL;
} ?></textarea>
<p class="description">
<?php esc_html_e('Visits and pageviews from any of these IP addresses will be ignored.', 'koko-analytics'); ?>
<?php echo ' '; ?>
<?php esc_html_e('Enter each IP address on its own line.', 'koko-analytics'); ?>
<?php echo ' '; ?>
<?php printf(esc_html__('Your current IP address is %s.', 'koko-analytics'), '<code>' . $_SERVER['REMOTE_ADDR'] . '</code>'); ?>
</p>
</div>

<div class="ka-margin-m">
<fieldset>
<legend class="ka-settings--label"><?php esc_html_e('Use cookie to determine unique visitors and pageviews?', 'koko-analytics'); ?></legend>
Expand Down

0 comments on commit 47cdbad

Please sign in to comment.