Logo


Tracking adsense for search queries

Posted in php, Programming by Dragan on the November 2nd, 2007

Tutorial on how to log all your search queries when you use Adsense for Search

Note:

1)This is slightly advanced tutorial, I didn't get into details too much, intermediate/advanced knowledge of php/mysql is required here.

2)This example uses ADOdb as database abstraction layer, ADOdb has a great manual so you should read it if you have any problems

3) Yes I know that Google Adsense already has Top Queries report, goal of this tutorial is not just to show you plain stats for search but to provide you data that you may use for more complex analysis.

Adsense for search

First when you setup your adsense for search choose Open search results within my own site

After you successfully setup your adsense for search you should access your search result pages via this kind of URL:

mydomain.com/search-results-url/?q=query+string&domains=....

(you should get one really long URL)

now all we must do is to get query string from request URL and store it into database.
In order to do so we first create table to hold query summary:

CREATE TABLE `keywords_stats` (
  `id` mediumint(8) unsigned NOT NULL auto_increment,
  `keyword` varchar(128) character set utf8 NOT NULL,
  `cnt` mediumint(8) unsigned NOT NULL default '1',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `keyword` (`keyword`)
  ) ENGINE=MyISAM DEFAULT CHARSET=latin1

Now this is fine, we have place to put our queries but what if we want to make application that not only track queries, but also enables us to track queries on given date or in a time range? Second table to the rescue:

(this enables us to have query stats by date)

CREATE TABLE `keywords_stats_byday` (
  `id` mediumint(8) unsigned NOT NULL auto_increment,
  `keyword_id` mediumint(9) NOT NULL,
  `query_date` date NOT NULL,
  `cnt` mediumint(9) NOT NULL default '1',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `keyword_id` (`keyword_id`,`date`)
  ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1

Next, we create code that reads q parameter from URL and writes it int database

<?php

require_once('pathh/to/adodb.inc.php');
//CHANGE THIS TO YOUR DATABASE SETTINGS
$db_username='root';      //mySQL username
$db_password='pass';    		  //MySQL password
$db_name='test';       //MySQL database name
$db_host='127.0.0.1';     //MySQL host name or IP

$db = &ADONewConnection('mysql');
$db->Connect($db_host, $db_username, $db_password, $db_name);

$db->Execute("SET NAMES utf8");
$query_string=mysql_real_escape_string(str_replace('+',' ',$_GET['q']));

$db->Execute("insert into keywords_stats (`id`,`keyword`) values (NULL,'$query_string') ON DUPLICATE KEY UPDATE cnt=cnt+1;");
$keywordID=$db->getRow("select id from keywords_stats where keyword='$query_string'; ");
$db->Execute("insert into keywords_stats_byday (`id`,`keyword_id`,`query_date`) values (NULL,'$keywordID[id]',CURDATE()) ON DUPLICATE KEY UPDATE cnt=cnt+1;");

?>

now we sould save this php code as queries.php and include it somewhere on your search result page.


<?php
    require_once('queries.php');
?>

<div style="width:805px;margin:40px auto;">
<!-- Google Search Result Snippet Begins -->
<div id="googleSearchUnitIframe"></div>
<script type="text/javascript">
.
.
.
//(rest of search results code)

you can test your script by pointing your browser to:

mydomain.com/path-to/queries.php?q=somestring

somestring should appear in your tables.

now all that is left is to build application that show query stats.

SQL query to get all time top 20 keywords

SELECT keyword, cnt
FROM `keywords_stats`
ORDER BY cnt DESC
LIMIT 0 , 20

SQL query to get top 20 keywords today:

SELECT keywords_stats.keyword, sum( keywords_stats_byday.cnt ) AS wordcount
FROM keywords_stats
LEFT JOIN keywords_stats_byday ON keywords_stats.id = keywords_stats_byday.keyword_id
WHERE query_date = CURDATE( )
GROUP BY keywords_stats_byday.query_date, keyword_id
ORDER BY wordcount DESC
LIMIT 0 , 20

You may also get query keyword percentage, queries in some data range (good for export to excel, or to draw graphs).

Resources:

19 Responses to 'Tracking adsense for search queries'

Subscribe to comments with RSS or TrackBack to 'Tracking adsense for search queries'.

  1. Kaamajakaaya said,

    on February 12th, 2009 at 5:26 pm

    Nice post! GA is also my biggest earning. However, it’s not a much.

  2. ÇÛÊÕÇÈ said,

    on March 3rd, 2009 at 6:05 am

    sry i just know how to write my name in arabic :) ) my bad english donot help to type here but really thanks.

  3. Jason said,

    on March 7th, 2009 at 9:11 am

    thanks !! very helpful post!


  4. on March 7th, 2009 at 10:16 am

    There is obviously a lot to know about this. I think you made some good points here.

  5. Steve Rawlinson said,

    on March 12th, 2009 at 9:14 pm

    In the second table CREATE statement it should be "query_date" not "date"

    UNIQUE KEY `keyword_id` (`keyword_id`,`query_date`)


  6. on April 28th, 2009 at 4:07 pm

    Your blog is so informative … keep up the good work!!!!

  7. Mel Y. said,

    on May 1st, 2009 at 1:26 am

    I never ever post but this time I will,Thanks alot for the great blog :)


  8. on May 6th, 2009 at 5:11 pm

    What is captcha code?, pls provide me captcha code codes or plugin, Thanks in advance.


  9. on May 21st, 2009 at 3:33 am

    I just have to post about this, but this jogged my memory of the just released news with the CEO of Craigslist wanting for an apology from the AG of South Carolina. Always something different with Craigslist and things in the news.


  10. on July 6th, 2009 at 12:29 pm

    There is obviously a lot to know about this. I think you made some good points in Features also.


  11. on July 14th, 2009 at 9:12 pm

    To start earning money with your blog, initially use Google Adsense but gradually as your traffic increases, keep adding more and more money making programs to your site.

  12. Aaron said,

    on July 19th, 2009 at 11:16 pm

    Anybody know if I wanted to bookmark this post do I have to join Sphinn first?

  13. bunkbed said,

    on September 22nd, 2009 at 9:09 pm

    Great Deal!! Good Information.

  14. income said,

    on September 23rd, 2009 at 5:03 am

    Keep working ,great job! This was what I needed to know.

  15. Cruise said,

    on October 4th, 2009 at 5:12 am

    Found some Google Wave invites, let me know if you need one stevencruisest@gmail.com.

  16. hot cam said,

    on November 6th, 2009 at 5:07 pm

    I usually don’t post in Blogs but your blog forced me to, nice work.. beautiful ;)

  17. Martha said,

    on April 17th, 2010 at 1:45 am

    nice site I like and I sure to visit, well done


  18. on May 5th, 2010 at 12:13 pm

    Your blog is so informative … keep up the good work!!!

  19. Steve said,

    on August 15th, 2010 at 10:16 am

    This is good stuff...right here. thanks for this

Leave a Reply (English only)


All Rights Reserved, Copyright © 2007 Dragan Bajcic.
YourTree | Dragan@YourTree | Web Development and Consulting