Skip to content

Instantly share code, notes, and snippets.

@mtoensing
Last active January 13, 2017 14:42
Show Gist options
  • Save mtoensing/a9df8174c5923959dd9948cf9125b142 to your computer and use it in GitHub Desktop.
Save mtoensing/a9df8174c5923959dd9948cf9125b142 to your computer and use it in GitHub Desktop.
Remove duplicates of AMP and comment pages from Google Analytics Top Content Widget
<?php
/**
* Plugin Name: Google Analytics Widget - Remove AMP and comment page duplicates
* Contributors: MarcDK
* Donate link: http://paypal.me/marctoensing
* Description: Remove AMP and comment pages from google analytics widget
* Plugin URI: https://gist.github.com/mtoensing/a9df8174c5923959dd9948cf9125b142/
* Version: 1.0
*/
// http://stackoverflow.com/a/10514539/149761
function super_unique($array, $key)
{
$temp_array = array();
foreach ($array as &$v) {
if (!isset($temp_array[$v[$key]])) {
$temp_array[$v[$key]] =& $v;
}
}
$array = array_values($temp_array);
return $array;
}
function gtc_pages_filter_remove_duplicated_items($pages)
{
$strings_to_exclude = array(
'/amp/',
'/comment-page-',
'/blog/'
);
foreach ($pages as $index => &$page) {
// get position of each string in array
foreach ($strings_to_exclude as $string) {
$last_string_start = strrpos($page['path'], $string);
if ($last_string_start !== false) {
$overflow = substr($page['path'], $last_string_start + 1, strlen($page['path']) );
$page['path'] = str_replace($overflow, "", $page['path']);
}
}
}
$pages = super_unique($pages,'path');
return $pages;
}
add_filter('gtc_pages_filter', 'gtc_pages_filter_remove_duplicated_items');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment