Javascript
Adwords query tracking
Do you also want to know which queries people are using to find you? If you are using Google Analytics the queries from Google organic will show up in here. If you have some Google Adwords campaign then the searchquery is not tracked.
I've got the solution for this. In my <head>.....</head>
I have a global.js javascript file where my urchincode is placed.
You also can see the following script. This script is not in the body of a function so this script will execute immediately.
The Script
_uacct = "**-******-*"; // your urchin code generated by Google Analytics if( document.referrer ) { if( document.referrer.indexOf( 'google' ) != -1 ) { var urlPattern = /(\?|&)q=([^&]*)/; var aMatches = urlPattern.exec( document.referrer ); if( aMatches != null ) { urchinTracker( '/query/' + aMatches[2] ); } else { urchinTracker(); } } else { urchinTracker(); } } else { urchinTracker(); }
This script will first check if the referrer is set
if( document.referrer )
If the referrer exists, it checks the referrer with the following pattern: /(\?|&)q=([^&]*)/
.
If you are not familiar with regular expressions than you
should look for some explenations or read the following page on PHP.net.
It's very powerfull stuff.
var aMatches = urlPattern.exec( document.referrer );
The pattern examines the referrer string and places 3 elements in the aMatches
array.
The first element of the array or aMatches[0]
is the total referrer string. This is the same as document.referrer
.
The second element holds the first match and the third element holds the second match defined in the pattern.
If the referrer doesn't match at all the RegExPattern will return null
.
if( aMatches != null ) { urchinTracker( '/query/' + aMatches[2] ); } else { urchinTracker(); }
So we check if the variable aMatches
is not null. If this is true then give the third element of the aMatches
as argument with the urchinTracker function. The third element is the searchquery of the user that found your website.