Posted in ColdFusion | Posted on 07-20-2009 | 2,411 views
Yesterday I blogged about I how I integrated Todd Sharp's ColdFusion/Google API project into RIAForge. I had a few people ask to see a bit of the code, so I thought I'd share how it was added. Credit goes to Todd Sharp for the integration, I just handled the view, and obviously, to see his core Google/CF code, visit his project.
Ok, so to start off with, we added a ColdSpring bean definition for his service:
2 <constructor-arg name="username">
3 <value>rcamden@gmail.com</value>
4 </constructor-arg>
5 <constructor-arg name="password">
6 <value>mypassword</value>
7 </constructor-arg>
8 <constructor-arg name="defaultFormat">
9 <value>xml</value>
10 </constructor-arg>
11</bean>
As you can see, the API is initialized with my login for Google Analytics. Yes, that's my real password. Go ahead and try it.
Next up is the controller method. This runs the reports I asked Todd to set up. To be clear, these are the reports I wanted added to RIAForge, but do not represent all the data you can get from Google Analytics.
2output="false">
3 <cfargument name="event" type="any" />
4 <cfset var anal = getModelGlue().getBean("analytics") />
5 <cfset var id = "ga:1392225" /><!--- hardcoding - make dynamic if you
6want to --->
7 <cfset var projectOb = arguments.event.getValue("project")>
8 <cfset var project = lcase(projectOb.getUrlName()) & ".riaforge.org" />
9 <cfset var downloadpage = "/index.cfm?event=action.download" /><!---
10set it as a variable in case it ever changes --->
11 <cfset var trafficSources =
12anal.getAnalyticsData(id=id,dimensions="ga:hostname,ga:source,ga:referralPath",metrics="ga:pageViews,ga:visits,ga:newVisits,ga:timeOnSite",filters="ga:hostname=~^#left(project,
1321)#", sort="-ga:visits",maxResults=25) />
14 <cfset var keywords =
15anal.getAnalyticsData(id=id,dimensions="ga:hostname,ga:keyword",metrics="ga:pageViews,ga:visits,ga:newVisits,ga:timeOnSite",filters="ga:hostname=~^#left(project,
1621)#", sort="-ga:visits",maxResults=25) />
17 <cfset var downloads =
18anal.getAnalyticsData(id=id,dimensions="ga:hostname,ga:pagePath,ga:month,ga:year",
19metrics="ga:pageViews",filters="ga:hostname=~^#left(project,21)#;ga:pagePath==#downloadpage#",startDate=dateAdd("m",-11,now()))
20/>
21 <cfset var views =
22anal.getAnalyticsData(id=id,dimensions="ga:hostname,ga:month,ga:year",metrics="ga:pageViews,ga:visits,ga:newVisits,ga:timeOnSite",filters="ga:hostname=~^#left(project,
2321)#",startDate=dateAdd("m",-11,now())) />
24 <!--- you might consider caching some of this stuff for performance
25reasons and because there are rate limits to the google api --->
26 <cfset arguments.event.setValue("trafficSources", trafficSources) />
27 <cfset arguments.event.setValue("keywords", keywords) />
28 <cfset arguments.event.setValue("downloads", downloads) />
29 <cfset arguments.event.setValue("views", views) />
30</cffunction>
I want to first point out this line:
This is a hard coded account id for RIAForge. My Google Analytics account has about 10 sites being tracked, and this one specifically focuses it on RIAForge.
After that we simply run four getAnalyticsData calls. I won't go over each call, but you can see how different options, filters, etc, are passed in to get the appropriate data.
And really, that's it! I did have to massage the data a bit in the view. First, the query was sorted newest date to oldest. I also translated the date based columns (one for month and one for year) into one formatted date column. But outside of that, the hard work was already done for me.
Anyway, I hope this helps others and encourages folks to check out the project.


idea = id
I/O Exception: peer not authenticated
i looked into it a bit and people are saying that error is normally that the JVM needs the site's SSL cert added to it's keystore, but it seems strange that google's SSL would require such a hack as they'd be using a tier 1 cert provider surely.
did you experience anything like this as i'm sure you are on a similar configuration to me: OSX 10.5.7, cf 8,0,1,195765, jvm 1.6.0_13
thanks
analyticsTestPublic.cfm that comes with the coldfusion google api, but can not figure our how to do anything other than cfdump them. I also tried to copy your code above and change out the customizations to make it work for mine and I can not get anything but the bean info to output. Do you know of any other examples of using the google api and cutomzing output.
Following up on your's and Marc's conversation, can you explain why the following code isn't working for me?
<cfchart format="flash" show3d="true" chartwidth="680" chartheight="300" pieslicestyle="solid" title="Top 10 pages" style="#style#">
<cfchartseries type="pie" query="getContentOverview" valueColumn="pageviews" itemColumn="pagepath" />
</cfchart>
I've added this to the bottom of the analyticstestpublic.cfm file and keep getting an error... I've tried changing the query name to all variants of above that seemed relevant and keep getting the "Query not found: getContentOverview ".
Thanks!
<cfdump var="#getContentOverview#">
what do you see?
the test page I'm working on is:
http://www.danhynes.com/pubtoolMySQL/google/analyt...
and as you'll see both the struct and query are there and have data... I just cant figure out why I cant access the query...
thanks so much for your help and this killer component!
I actually originally pasted the wrong version of the code... here it is with the query set to the "#contentOverview#" dump... I added the label you asked about and here is the pastebin link of my source:
http://pastebin.com/m55ca107f
live site file updated and link is same as above:
http://www.danhynes.com/pubtoolMySQL/google/analyt...
I greatly appreciate your help and patience sir!
contentoverview.data
As your query.
I was misunderstanding the structure of what the dump was giving me... Cannot thank you enough sir!
[Add Comment] [Subscribe to Comments]