Created
January 28, 2019 21:29
-
-
Save Zodiac1978/093244d0b3837cc83c37c2a2a2dc15ea to your computer and use it in GitHub Desktop.
Skip tracking for specific domains from the Community-contributed list of referrer spammers from Matomo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Kill Referrer Spam for Statify | |
* Description: Skip tracking for domains from the Community-contributed list of referrer spammers from Matomo. | |
* Plugin URI: http://torstenlandsiedel.de | |
* Version: 1.0 | |
* Author: Torsten Landsiedel | |
* Author URI: http://torstenlandsiedel.de | |
* Licence: GPL 2 | |
* License URI: http://opensource.org/licenses/GPL-2.0 | |
* | |
* @package Statify\Kill Referrer Spam | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} // Exit if accessed directly | |
/** | |
* Checks referrer and compares with blacklist from Github file | |
* | |
* @return boolean Returns if tracking is skipped or not. | |
*/ | |
function apply_referrer_spammer_list() { | |
// Get Referrer. | |
$referrer = wp_parse_url( wp_get_raw_referer() ); | |
$referrer = strtolower( ( isset( $referrer['host'] ) ? $referrer['host'] : '' ) ); | |
// Bail early if no referrer there. | |
if ( ! $referrer ) { | |
return false; | |
} | |
// Get blacklist from Github. | |
// TODO: Proof of concept. This needs some sort of caching for production. | |
$blacklist = file( 'https://raw.githubusercontent.com/matomo-org/referrer-spam-blacklist/master/spammers.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ); | |
// Check blacklist. | |
if ( in_array( $referrer, $blacklist, true ) ) { | |
return true; | |
} | |
return false; | |
}; | |
add_filter( 'statify__skip_tracking', 'apply_referrer_spammer_list' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment