Thursday, January 26, 2006

Re: Another greasemonkey script....

Here's the first attempt...

// ==UserScript==
// @name Google Analytics Detector
// @namespace http://codeword.blogspot.com
// @description Detects if a page uses Google Analytics
// @include *
// ==/UserScript==

var scripts = document.getElementsByTagName( "script" );

var refPresent = false, codePresent = false;

for ( var i = 0; i < scripts.length; ++i ) {
var script = scripts[ i ];

// Check reference if not already found
if ( !refPresent ) {
var ref = script.src;

if ( ref != null ) {
var URL = "http://www.google-analytics.com/urchin.js";

refPresent = ( ref.search( URL ) != -1 );

GM_log( "Tested ref: " + ref + " Result: " + refPresent );
}
}

// Check code if not already found
if ( !codePresent ) {
var code = script.innerHTML;

if ( code != null ) {
var ACCT = "_uacct";
var TRACKER = "urchinTracker()";

codePresent = ( ( code.search( ACCT ) != -1 ) && ( code.search( TRACKER ) != -1 ) );

GM_log( "Tested code: " + code + " Result: " + codePresent );
}
}
}

if ( refPresent && codePresent ) {
alert( "This smart bastard is using Google Analytics" );
}


Save as analyticsDetector.user.js, open in Firefox and then "Install This User Script..."

Couple things... Besides the code snippet Rahul posted, there is also an external js file that is referenced (http://www.google-analytics.com/urchin.js). I check that both code as well as reference are present. Also in the code, I think "_udn" is optional. It's there on slashdot, but not on codeword, so I don't check for that. Didn't test this very much. Just on codeword, slashdot (detected) and yahoo, microsoft (undetected).

Any improvements?

No comments: