Skip to content

Instantly share code, notes, and snippets.

@sybrew
Last active April 25, 2019 17:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sybrew/ab5553dd8a06e73794680c4a2cc24661 to your computer and use it in GitHub Desktop.
Save sybrew/ab5553dd8a06e73794680c4a2cc24661 to your computer and use it in GitHub Desktop.
Search Canonical Push for Google Analytics
<?php
/**
* Plugin Name: Search Canonical Push for Google Analytics
* Plugin URI: https://theseoframework.com/
* Description: This plugin adds a small script to Search archives that optimizes the Google Analytics script when using pretty Search links.
* Version: 1.0.1
* Author: Sybre Waaijer
* Author URI: https://theseoframework.com/
* License: GPLv3
*/
/**
* Search Canonical Push for Google Analytics
* Copyright (C) 2017 Sybre Waaijer, CyberWire (https://cyberwire.nl/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Generates a script that is injected very early.
* It adjusts the Google Analytics 'send' script to believe there's a search parameter.
* This will allow Google Analytics users to capture and analyze site search queries.
*
* This script will listen to DOMnode insertion changes, and will overwrite the
* 'send' function of Google Analytics when found.
* That adjustment forces an extra parameter to comply to the search results, when
* possible.
*
* After it's done, it will stop listening.
*
* @since 1.0.0
* @return void
*/
\add_action( 'wp_head', function() {
if ( ! \is_search() || empty( $GLOBALS['wp_rewrite']->get_search_permastruct() ) )
return;
$q = \get_search_query();
$lr = \set_url_scheme( \get_search_link( $q ), 'relative' );
$s_ga_lr = \esc_js( $lr . '?s=' . urlencode( $q ) );
//= Already escaped.
echo <<<GAFIX
<script>document.addEventListener("DOMNodeInserted",function a(){if("ga"in window){var b=window.ga;window.ga=function(){3>arguments.length&&"send"===arguments[0]&&"pageview"===arguments[1]&&[].push.call(arguments,"$s_ga_lr");b.apply(function(){},arguments)};document.removeEventListener("DOMNodeInserted",a)}});</script>\n
GAFIX;
}, ~ PHP_INT_MAX );
/*== Javascript source ==
document.addEventListener( 'DOMNodeInserted', function a() {
if ( ! ( 'ga' in window ) ) return;
var originalMethod = window['ga'];
window['ga'] = function () {
if ( arguments.length < 3 ) {
if ( 'send' === arguments[0] && 'pageview' === arguments[1] ) {
[].push.call( arguments, "$s_ga_lr" );
}
}
originalMethod.apply( function() {}, arguments );
};
document.removeEventListener( 'DOMNodeInserted', a );
} );
==/ Javascript source ==*/
@sybrew
Copy link
Author

sybrew commented Jul 1, 2017

Tested for two weeks. GA doesn't seem to interpret the data.
I will look at this today.

Edit: The previous version assumed the wrong method. It should work now.

@sybrew
Copy link
Author

sybrew commented Jul 4, 2017

I forgot to mention: this snippet requires PHP 5.5 or later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment