ASP Adsense Tracker
Here is the code for the ASP Adsense Tracker. The first part is the ASP Tracking config file and the second part is the ASP Tracking program.
ASP Adsense Tracking (config file) - Copy it and paste it into adsense_config.asp:
<%
' See adsense.asp for licensing terms.
' CONFIGURE THE VARIABLES BELOW THIS LINE
' ------------------------------------------------------------
AdSense_UserName = "you@yourhost.com"
AdSense_Password = "yourpassword"
AdSense_DatabasePath = "C:\adsense\adsense.mdb"
' Set your time zone below
' For example, if you are in EST (GMT-5:00)
' then set the AdSense_LocalTimeZone to -5.
AdSense_LocalTimeZone = -5
' ------------------------------------------------------------
' END OF CONFIGURATION SECTION
%>
The following is the ASP Adsense Tracking Program - Copy it and paste into adsense.asp:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<% Option Explicit %>
<%
'
' Adsense Statistics Viewer - Displays tracking database statistics.
' Shailesh N. Humbad and Geoff Haynes, Copyright (C) 2004-2008
' Version 6.2 - Works with Adsense Tracking Script version 6.2
'
' http://www.monetizers.com/ - Search Engine Optimization
' http://www.atensoftware.com/ - Professional Website Development
'
' LICENSE
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.
'
' 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.
' http://www.opensource.org/licenses/gpl-license.php
'
' CONTENTS
' adsense.asp - A webpage to display statistics from the adsense
' tracking script database. The page automatically refreshes
' each hour. There is an auto-login button and range filter.
' adsense_config.asp - Contains configuration variables.
'
' INSTRUCTIONS
' 1. Copy both files to a web directory, like c:\inetpub\wwwroot.
' 2. Apply read permissions for IUSR_<MachineName> to the files, if needed.
' 3. Edit the adsense_config.asp file and enter your information.
' 4. View the webpage in your web browser, for example http://localhost/adsense.asp.
'
' NOTES
' You may not be allowed to post your statistics in a public
' location due to Google Adsense Confidentiality Terms.
' Disable page caching
Response.Expires = 0
Response.Expiresabsolute = Now() - 1
Response.AddHeader "pragma","no-cache"
Response.AddHeader "cache-control","private"
Response.CacheControl = "no-cache"
' Declare all variables
Dim AdSense_UserName, AdSense_Password, AdSense_DatabasePath, AdSense_LocalTimeZone
Dim objFSO, objRS
Dim strsql, StatsRange
Dim DatabasePath, StyleSwitch, strWhereClause, DaysSinceStartOfMonth
Dim GoogleOffset, PSToffset, MonthlyEarnings
' Load the configuration variables
%><!-- #include file="adsense_config.asp" --><%
' Check if the database file exists.
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If Not objFSO.FileExists(AdSense_DatabasePath) Then
%><html><body><center><h2 style="color:darkblue;">Adsense Statistics Configuration Error</h2>
<h4>The database file '<%=AdSense_DatabasePath%>' does not exist.<br>Please configure by editing the adsense_config.asp file in notepad.</h4></center></body></html><%
Set objFSO = Nothing
Response.End
End If
Set objFSO = Nothing
%>
<html>
<head><title>Adsense Statistics Viewer</title>
<style type="text/css">
table.adsense {
border-collapse:collapse;
padding:1px;
margin: auto;
}
table.adsense th {
font-size: 9pt;
border: 1px solid #666666;
padding: 1px;
color: black;
background-color: #CCCCCC;
}
table.adsense td.statcontrols {
font-size: 10pt;
border: 1px solid #666666;
color: black;
background-color: #F0F0F0;
}
table.adsense th.tabletitle {
font-size: 12pt;
border: 1px solid #666666;
color: black;
background-color: #F0F0F0;
}
table.adsense td {
font-size: 9pt; padding: 1px;
border: 1px solid #666666;
color: black;
white-space: nowrap;
}
table.adsense tr.d0 td {
background-color: #FFFFFF;
text-align:right;
}
table.adsense tr.d1 td {
background-color: #FEF7FF;
text-align:right;
}
body {
font-family: 'Arial','Helvetica',sans-serif;
margin: 0px;
padding: 0px;
}
label {
background-color: #E7E7E7;
color: black;
}
</style>
<script type="text/javascript">
<!--
var lastHour;
var refreshMinute = 59;
var refreshSecond = 20;
// This function refreshes the page
// at five minutes and twenty seconds past each hour.
function TimedPageRefresher(){
var currentTime;
currentTime = new Date();
if(currentTime.getMinutes() >= refreshMinute
&& currentTime.getSeconds() >= refreshSecond &&
lastHour != currentTime.getHours()) {
lastHour = currentTime.getHours();
document.location.reload();
}
window.setTimeout("TimedPageRefresher()",1000)
}
function initpage() {
var currentTime;
currentTime = new Date();
lastHour = currentTime.getHours();
if(currentTime.getMinutes() < refreshMinute ||
(currentTime.getMinutes() == refreshMinute
&& currentTime.getSeconds() < refreshSecond))
{
lastHour--;
}
TimedPageRefresher();
}
// -->
</script>
</head>
<body onload="initpage();">
<%
Set objRS = Server.CreateObject("ADODB.RecordSet")
StatsRange = Trim(LCase(Request.QueryString("StatsRange")))
If StatsRange = "" Then
StatsRange = "last2"
End If
PSToffset = -8
If Request.QueryString("GoogleTime") = "checked" Then
GoogleOffset = (PSToffset-Adsense_LocalTimeZone) * 60
Else
GoogleOffset = 0
End If
Select Case StatsRange
Case "today"
strWhereClause = " WHERE DateValue(DateAdd('n'," & GoogleOffset & ",QueryDate)) = DateValue(DateAdd('n'," & GoogleOffset & ",Now())) "
Case "last2"
strWhereClause = " WHERE DateValue(DateAdd('n'," & GoogleOffset & ",QueryDate)) > DateValue(DateAdd('d',-1,DateAdd('n'," & GoogleOffset-1440 & ",Now()))) "
Case "last7"
strWhereClause = " WHERE DateValue(DateAdd('n'," & GoogleOffset & ",QueryDate)) > DateValue(DateAdd('d',-6,DateAdd('n'," & GoogleOffset-1440 & ",Now()))) "
Case "last30"
strWhereClause = " WHERE DateValue(DateAdd('n'," & GoogleOffset & ",QueryDate)) > DateValue(DateAdd('d',-29,DateAdd('n'," & GoogleOffset-1440 & ",Now()))) "
Case "sameweekday"
strWhereClause = " WHERE WeekDay(DateAdd('n'," & GoogleOffset & ",QueryDate)) = WeekDay(DateAdd('n'," & GoogleOffset & ",Now())) "
Case "all"
strWhereClause = ""
End Select
strsql = "SELECT AdsenseID, QueryDate, PageImpressions, AdUnitImpressions, AdClicks, AdEarnings, SearchPageImpressions, SearchClicks, SearchEarnings, AdMonthlyEarnings, SearchMonthlyEarnings, QueryNote FROM tblAdsense " & strWhereClause & " ORDER BY QueryDate DESC"
%>
<table align="center" cellpadding="3" class="adsense">
<tr>
<th nowrap class="tabletitle" align="center">
Adsense Statistics
<form action="https://www.google.com/accounts/ServiceLoginBoxAuth" name="login" method="post" style="display:inline;">
<input type="hidden" name="service" value="adsense">
<input type="hidden" name="hl" value="en_US">
<input type="hidden" name="alwf" value="true">
<input type="hidden" name="rm" value="hide">
<input type="hidden" name="nui" value="15">
<input type="hidden" name="fpui" value="3">
<input type="hidden" name="ifr" value="true">
<input type="hidden" name="continue" value="https://www.google.com/adsense/login-box-gaiaauth">
<input type="hidden" name="followup" value="https://www.google.com/adsense/login-box-gaiaauth">
<input type="hidden" name="ltmpl" value="login">
<input type="hidden" name="GA3T" value="Zgpc8dqf81w">
<input type="hidden" name="Email" value="<%=AdSense_UserName%>">
<input type="hidden" name="Passwd" value="<%=AdSense_Password%>">
<input type="submit" value="AdSense Login">
</form>
</th>
<th nowrap align="center">
<%= DateAdd("n", GoogleOffset, Now()) %>
</th>
</tr>
<tr>
<td class="statcontrols">
<form method="get" name="tablecontrolform" action="adsense.asp">
<label for="r_today"><input onclick="document.forms['tablecontrolform'].submit();" name="StatsRange" id="r_today" type="radio" value="today" <%If StatsRange="today" Then Response.Write("checked")%>>
Today </label>
<label for="r_last2"><input onclick="document.forms['tablecontrolform'].submit();" name="StatsRange" id="r_last2" type="radio" value="last2" <%If StatsRange="last2" Then Response.Write("checked")%>>
Last 2 days </label>
<label for="r_last7"><input onclick="document.forms['tablecontrolform'].submit();" name="StatsRange" id="r_last7" type="radio" value="last7" <%If StatsRange="last7" Then Response.Write("checked")%>>
Last 7 days </label>
<label for="r_last30"><input onclick="document.forms['tablecontrolform'].submit();" type="radio" name="StatsRange" id="r_last30" value="last30" <%If StatsRange="last30" Then Response.Write("checked")%>>
Last 30 days </label>
<label for="r_sameweekday"><input onclick="document.forms['tablecontrolform'].submit();" type="radio" name="StatsRange" id="r_sameweekday" value="sameweekday" <%If StatsRange="sameweekday" Then Response.Write("checked")%>>
Same weekday </label>
<label for="r_all"><input onClick="document.forms['tablecontrolform'].submit();" type="radio" name="StatsRange" id="r_all" value="all" <%If StatsRange="all" Then Response.Write("checked")%>> All </label>
</td>
<td class="statcontrols" align="center">
<label for="GoogleTime"><input name="GoogleTime" type="checkbox" id="GoogleTime" onclick="document.forms['tablecontrolform'].submit();" value="checked" <%=Request.QueryString("googletime")%>>
Google Time </label>
</td>
</tr>
</table>
<table class="adsense">
<tr>
<th rowspan="3">Date
<% If GoogleOffset <> 0 Then Response.Write("<br>(Google Time)")%></th>
<th colspan="9">Ads</th>
<th colspan="6">Search</th>
<th colspan="3">Aggregate</th>
</tr>
<tr>
<th colspan="3">Pages</th>
<th colspan="3">Ad Units</th>
<th rowspan="2">Clks</th>
<th rowspan="2">Day</th>
<th rowspan="2">Month</th>
<th rowspan="2">Impr</th>
<th rowspan="2">CTR</th>
<th rowspan="2">CPM</th>
<th rowspan="2">Clks</th>
<th rowspan="2">Day</th>
<th rowspan="2">Month</th>
<th rowspan="2">Per<br>Day</th>
<th rowspan="2">Month</th>
<th rowspan="2">Day</th>
</tr>
<tr>
<th>Impr</th>
<th>CTR</th>
<th>CPM</th>
<th>Impr</th>
<th>CTR</th>
<th>CPM</th>
</tr>
<%
' Print the statistics
objRS.Open strsql, "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & AdSense_DatabasePath, adOpenForwardOnly, adLockReadOnly, adCmdText
StyleSwitch = 0
Do While Not objRS.EOF
StyleSwitch = StyleSwitch Xor 1
Response.write "<tr class=""d" & StyleSwitch & """>"
%><td><%=DateAdd("n",GoogleOffset,objRS(1))%></td><%
If Trim(objRS(11)) <> "" Then
Response.write "<td colspan=""18"">"
Response.write Server.HTMLEncode(objRS(11))
Response.write "</td>"
Else
' Pages
Response.write "<td>"
Response.write objRS(2)
Response.write "</td>"
Response.write "<td>"
Response.write GetCTR(objRS(2), objRS(4))
Response.write "</td>"
Response.write "<td>"
Response.write GetCPM(objRS(2), objRS(5))
Response.write "</td>"
' Ad units
Response.write "<td>"
Response.write objRS(3)
Response.write "</td>"
Response.write "<td>"
Response.write GetCTR(objRS(3), objRS(4))
Response.write "</td>"
Response.write "<td>"
Response.write GetCPM(objRS(3), objRS(5))
Response.write "</td>"
' Ads summary
Response.write "<td>"
Response.write objRS(4)
Response.write "</td>"
Response.write "<td>"
Response.write FormatCurrencyDB(objRS(5))
Response.write "</td>"
Response.write "<td>"
Response.write FormatCurrencyDB(objRS(9))
Response.write "</td>"
' Search
Response.write "<td>"
Response.write objRS(6)
Response.write "</td>"
Response.write "<td>"
Response.write GetCTR(objRS(6), objRS(7))
Response.write "</td>"
Response.write "<td>"
Response.write GetCPM(objRS(6), objRS(8))
Response.write "</td>"
Response.write "<td>"
Response.write objRS(7)
Response.write "</td>"
Response.write "<td>"
Response.write FormatCurrencyDB(objRS(8))
Response.write "</td>"
Response.write "<td>"
Response.write FormatCurrencyDB(objRS(10))
Response.write "</td>"
' Aggregates
MonthlyEarnings = objRS(10) + objRS(9)
Response.write "<td>"
If GoogleOffset <> 0 Then
Response.Write FormatCurrencyDB(MonthlyEarnings / Day(objRS(1)))
Else
Response.Write FormatCurrencyDB(MonthlyEarnings / Day(DateAdd("n", (PSToffset-Adsense_LocalTimeZone) * 60, objRS(1))))
If Month(objRS(1)) <> Month(DateAdd("n", (PSToffset-Adsense_LocalTimeZone) * 60, objRS(1))) Then
Response.Write " *"
End If
End If
Response.write "</td>"
Response.write "<td>"
Response.write FormatCurrencyDB(MonthlyEarnings)
Response.write "</td>"
Response.write "<td>"
Response.write FormatCurrencyDB(objRS(8) + objRS(5))
Response.write "</td>"
End If
Response.write "</tr>"
objRS.MoveNext
Loop
objRS.Close
' Clean Up
Set objRS = Nothing
%>
</table>
</form>
<center>
<p style="font-size:10pt; font-style:italic;">* indicates that the daily average is for the month one after (or one before) the<br> month displayed in the date column (only applies in local time mode)</p>
<p style="font-size:10pt;">Brought to you by:<br><a href="http://www.monetizers.com/?refer=adsensestatsasp">monetizers.com - Search Engine Optimization</a><br>and <a href="http://www.atensoftware.com/">atensoftware.com - Professional Website Development</a></p>
</center>
</body>
</html><%
' Prints currency values, corrected for nulls.
Function FormatCurrencyDB(dbValue)
If IsNull(dbValue) Then
FormatCurrencyDB = FormatCurrency(0)
Else
FormatCurrencyDB = FormatCurrency(dbValue)
End If
End Function
Function GetCTR(Impressions, Clicks)
If Not IsNumeric(Clicks) Then
Clicks = 0
End If
If Not IsNumeric(Impressions) Then
Impressions = 0
End If
If Impressions = 0 Then
GetCTR = 0
Else
GetCTR = Round(Clicks / Impressions * 100, 2) & "%"
End If
End Function
Function GetCPM(Impressions, Earnings)
If Not IsNumeric(Earnings) Then
Earnings = 0
End If
If Not IsNumeric(Impressions) Then
Impressions = 0
End If
If Impressions = 0 Then
GetCPM = 0
Else
GetCPM = FormatCurrencyDB(Round(Earnings / (Impressions / 1000), 2))
End If
End Function
%>
That's it! Now, just take those files and place them on your ASP Server.
|