crribs.com

the blog of brad shultz, systems design, RETS development, ETL, scripting, and windows task scheduler stuff

Archive for the ‘scripting’ tag

Google Analytics Api with VBScript – Retrieving all Profile Numbers

with 2 comments

This vbscript will retrieve all of the website’s profile numbers for your google analytics account.  Borrowed some code from http://mikaelspage.blogspot.com/2009/08/excel-functions-for-fetching-data.html

Changed a lot of code myself.  Removed some functions there may be remnants of them in there.  It does work though, and seems to be the only example on the web of doing this with VBScript.  Enjoy.

‘**********************************************************************
‘*  Get Google Analytics Profiles
‘************************************************************************

Call Main()

Sub Main()

email = “youremail@gmail.com”
password = “password”
token = getGAauthenticationToken(email, password)

getGAaccountData token, “False”

End Sub

Sub getGAaccountData(authToken, includeHeaders)

If authToken = “Authentication failed” Then
getGAprofiles = “Authentication failed”
WScript.Echo “Authentication failed”
Exit sub
End If
tempFile = “c:\googleAnalytics_Temp.txt”
Set fso = CreateObject(“scripting.filesystemobject”)
If fso.FileExists(tempfile) Then fso.DeleteFile(tempFile)
Set fileTemp = fso.OpenTextFile(tempFile, 8, True)

fileTemp.WriteLine chr(34) & “AccountName”“ , ”“ProfileNumber”“ , ”“ProfileTitle” & chr(34)

URL = “https://www.google.com/analytics/feeds/accounts/default”
Set objhttp = CreateObject(“MSXML2.ServerXMLHTTP”)

objhttp.Open “GET”, URL, False
objhttp.setRequestHeader “Content-type”, “application/x-www-form-urlencoded”
objhttp.setRequestHeader “Authorization”, “GoogleLogin Auth=” & authToken
objhttp.send (“”)

gaResponse = objhttp.responseText

If InStr(1, gaResponse, “Token invalid”) > 0 Or InStr(1, gaResponse, “Authorization required”) > 0 Then
getGAprofiles = “Authentication failed”
Exit sub
End If

Set XMLdoc = CreateObject(“MSXML2.DOMDocument”)
XMLdoc.LoadXML(objhttp.responseText)
Set theRoot = XMLdoc.DocumentElement
Set children = theRoot.ChildNodes

rivi = 1
For Each child In children
If child.nodeName = “openSearch:totalResults” Then
riveja = CInt(child.Text)
If includeHeaders = True Then
ReDim TempArray(riveja, 2)
Else
ReDim TempArray(riveja, 2)
End If
End If

If child.nodeName = “entry” Then

Set children2 = child.ChildNodes
For Each child2 In children2
If child2.nodeName = “dxp:property” Then
Set attribuutit = child2.Attributes
If attribuutit.getNamedItem(“name”).Text = “ga:accountName” Then TempArray(rivi, 0) = attribuutit.getNamedItem(“value”).text

End If

If child2.nodeName = “dxp:tableId” Then TempArray(rivi, 2) = CDbl(Replace(child2.Text, “ga:”, “”))

If child2.nodeName = “title” Then TempArray(rivi, 1) = child2.text

Next
fileTemp.Write chr(34) & TempArray(rivi, 0)
fileTemp.Write chr(34) & “ , ” & chr(34) & TempArray(rivi, 2)
fileTemp.Write chr(34) & “ , ” & chr(34) & TempArray(rivi, 1) & chr(34) & vbnewline
rivi = rivi + 1
End If

If includeHeaders = True Then
TempArray(0, 0) = “AccountName”
TempArray(0, 1) = “ProfileTitle”
TempArray(0, 2) = “ProfileNumber”
End If

getGAprofiles = TempArray

Next

End sub

Function getGAauthenticationToken(email, password)
If email = “” Then
getGAauthenticationToken = “”
Exit Function
End If

If password = “” Then
getGAauthenticationToken = “Input password”
Exit Function
End If

CurChr = 1

Set objhttp = CreateObject(“MSXML2.ServerXMLHTTP”)
URL = “https://www.google.com/accounts/ClientLogin”
objhttp.Open “POST”, URL, False
objhttp.setRequestHeader “Content-type”, “application/x-www-form-urlencoded”
objhttp.send (“accountType=GOOGLE&Email=” & email & “&Passwd=” & password & “&service=analytics&Source=InternetEngineKPI”)

authResponse = objhttp.responseText

If InStr(1, authResponse, “BadAuthentication”) = 0 Then
authTokenStart = InStr(1, authResponse, “Auth=”) + 4
authToken = Right(authResponse, Len(authResponse) - authTokenStart)
getGAauthenticationToken = authToken
Else

DTSTaskExecResult_Failure

End If

If Err.Number <> 0 Then
DTSTaskExecResult_Failure
End If
End Function

Function LastDayOFPreviousMonth(aDate)
LastDayOFPreviousMonth = DateAdd(“d”, -1, DateSerial(Year(aDate), Month(aDate), 1))
End Function

Function FirstDayOFPreviousMonth(aDate)
If Month(aDate) > 1 Then
FirstDayOFPreviousMonth = DateSerial(Year(aDate), Month(aDate) - 1, 1)
Else
FirstDayOFPreviousMonth = DateSerial(Year(aDate) - 1, 12, 1)
End If
End Function

Written by bshultz

May 26th, 2010 at 7:21 pm