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:

<bean id="analytics" class=""> <constructor-arg name="username"> <value></value> </constructor-arg> <constructor-arg name="password"> <value>mypassword</value> </constructor-arg> <constructor-arg name="defaultFormat"> <value>xml</value> </constructor-arg> </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.

<cffunction name="getAnalytics" access="public" returntype="void" output="false"> <cfargument name="event" type="any" /> <cfset var anal = getModelGlue().getBean("analytics") /> <cfset var id = "ga:1392225" /><!--- hardcoding - make dynamic if you want to ---> <cfset var projectOb = arguments.event.getValue("project")> <cfset var project = lcase(projectOb.getUrlName()) & "" /> <cfset var downloadpage = "/index.cfm?" /><!--- set it as a variable in case it ever changes ---> <cfset var trafficSources = anal.getAnalyticsData(id=id,dimensions="ga:hostname,ga:source,ga:referralPath",metrics="ga:pageViews,ga:visits,ga:newVisits,ga:timeOnSite",filters="ga:hostname=~^#left(project, 21)#", sort="-ga:visits",maxResults=25) /> <cfset var keywords = anal.getAnalyticsData(id=id,dimensions="ga:hostname,ga:keyword",metrics="ga:pageViews,ga:visits,ga:newVisits,ga:timeOnSite",filters="ga:hostname=~^#left(project, 21)#", sort="-ga:visits",maxResults=25) /> <cfset var downloads = anal.getAnalyticsData(id=id,dimensions="ga:hostname,ga:pagePath,ga:month,ga:year", metrics="ga:pageViews",filters="ga:hostname=~^#left(project,21)#;ga:pagePath==#downloadpage#",startDate=dateAdd("m",-11,now())) /> <cfset var views = anal.getAnalyticsData(id=id,dimensions="ga:hostname,ga:month,ga:year",metrics="ga:pageViews,ga:visits,ga:newVisits,ga:timeOnSite",filters="ga:hostname=~^#left(project, 21)#",startDate=dateAdd("m",-11,now())) /> <!--- you might consider caching some of this stuff for performance reasons and because there are rate limits to the google api ---> <cfset arguments.event.setValue("trafficSources", trafficSources) /> <cfset arguments.event.setValue("keywords", keywords) /> <cfset arguments.event.setValue("downloads", downloads) /> <cfset arguments.event.setValue("views", views) /> </cffunction>

I want to first point out this line:

<cfset var id = "ga:1392225" />

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.