Category Archives: Google Analytics

Google Analytics Showing My Site as Top Referrer

Update (Feb. 1, 2010) – Important:

After speaking to a few people regarding this issue, some other situations where this will occur have come to light.

First (most important), make sure you tracking code is installed properly on every page of your site. If you use cross-domain tracking or sub-domain tracking, install it correctly on every page of the site.

Second, do not mix the asynchronous tracking code with the original synchronous tracking code on the same page. This WILL result in tracking anomalies. Also, if you mix them on the same site but different pages, make sure they do the same thing. In other words, if one is set for cross-domain tracking, the other should be as well. And, finally, if you use third-party tools that incorporate GA tracking like some shopping carts, install the original tracking code unless specifically instructed to install the asynchronous code.

I began to notice that my own site, jensbits.com, is listed as the top referrer in Google Analytics. This did not seem to be correct since another site I managed had no such issue. And, I also determined that this began right after certain date, in my case, August 10, 2009. So I began to investigate.

What happened on or about 10 August 2009? I put a post on my site that day that has proven to be quite popular. In that post, there is a link to a demo. When I originally posted the demo link, I typed in an absolute URL of http://jensbits.com/demo/forms/index.php for the link’s href. Notice the missing ‘www.’ And, that was the culprit. Eventually, I went through my site to standardize my links and replaced all absolute paths with relative paths. It turns out, however, that loads of people apparently bookmarked the demo and were hitting it directly.

The cookies set by Google Analytics for the http://jensbits.com link had a host parameter (value) of ‘.jensbits.com.’ The cookie also had a correct referrer of (direct). So far so good. All is correct.

The demo page has a link on it to return to the post. That link is relative and it points back to the WordPress post. WordPress will correctly rewrite the URL to include the ‘www.’ The demo link was not a WordPress post so it did not get rewritten. After clicking on the ‘return to post’ link and looking at the cookies again, I now have double the cookies for my site. One set of cookies has ‘.jensbits.com’ as the host value and the other set has ‘www.jensbits.com’ as the value.

The cookies set by Google Analytics for the http://www.jensbits.com link had a referrer of jensbits.com. Correct, but incorrect.

I hate cookies

The solution was to fix the canonicalization of the URLs. In other words, all the URLs had to resolve to either jensbits.com or www.jensbits.com. I choose www.jensbits.com since WordPress was doing such a fine job for my posts.

Once the rewrite fix was in place, I have the correct amount of cookies and the correct referrer for the site when I retrace my steps from the demo link to the main site just as I did previously.

I love cookies

Don’t expect to see a precipitous drop off in the referrer number for your site once you institute this. It will decrease over time. Look for a trend, not a miracle. Google Analytics cookies have a lifespan. Return visitors may still carry erroneous cookie data like an albatross around their necks until it eventually dies a natural death.

BTW, the other site I mentioned, I had long ago put in a rewrite rule to include the ‘www’ because of a programmatic session cookie issue with a web-based application. It was my determination at the time that is was specific to the way the programming language I was using was setting cookies.

One other thing to consider: If you are the site owner and/or webmaster, you should put a filter into your Google Analytics profile that prevents your visits from being entered into the reports. An IP address filter is the preferred method to accomplish this.

Note: This is not a WordPress issue. This can happen to any site that will resolve to with or without the ‘www’ in the URL.

Recommended Reading

ColdFusion and Google Analytics: Getting Out What You Put In

The Hooking into Google Analytics post details how to connect to the Data Export API of Google Analytics (GA) and get a simple visitors count in return. Here we will expand greatly on that and create a cfc that will process just about any result from the API.

This code requires at least ColdFusion 8.0.1. Check your version before installing.

An auth token is needed for requests to the API and two ways to do that are the AuthSub and ClentLogin methods. The method used is up to the developer as once the auth token is retrieved and set into session, data calls to the API can be repeatedly made.

<cffunction name="googleLogin" access="public" hint="GA account authorization">
        <cfargument name="email" type="string" required="yes" default="">
        <cfargument name="password" type="string"required="yes" default="">
        <cfargument name="gaLoginUrl" type="string" required="no" default="https://www.google.com/accounts/ClientLogin">

        <cfset var loginAuth = "" />

        <cfhttp url="#arguments.gaLoginUrl#" method="post">
            <cfhttpparam name="accountType" type="url" value="GOOGLE">
            <cfhttpparam name="Email" type="url" value="#arguments.email#">
            <cfhttpparam name="Passwd" type="url" value="#arguments.password#">
            <cfhttpparam name="service" type="url" value="analytics">
            <cfhttpparam name="source" type="url" value="my-analytics">
        </cfhttp>

        <cfif NOT FindNoCase("Auth=",cfhttp.filecontent)>
            <cfset loginAuth = "Authorization Failed" />
        <cfelse>
            <cfset loginAuth = Mid(cfhttp.filecontent, FindNoCase("Auth=",cfhttp.filecontent) + (Len("Auth=")), Len(cfhttp.filecontent)) />
        </cfif>

         <cflock scope="session" type="exclusive" timeout="5">
                <cfset session.ga_loginAuth = loginAuth />
         </cflock>

    </cffunction>

To retrieve the profile or data feed, a call is made to the API via a function.

   <cffunction name="callApi" access="public" returntype="array" hint="GA data as array of structures">
        <cfargument name="gaUrl" type="string" required="yes">
        <cfargument name="authToken" type="string" required="no" default="#session.ga_loginAuth#" />

        <cfset var authTokenHeader = 'GoogleLogin auth=' & arguments.authToken />

        <cfset var responseOutput = "" />

        <cfhttp url="#arguments.gaUrl#" method="get">
            <cfhttpparam name="Authorization" type="header" value="#authTokenHeader#">
        </cfhttp>

        <cfset responseOutput = cfhttp.filecontent />
        <!---remove dxp: prefix from nodes that have it and strip xmlns from feed element--->
         <cfset responseOutput = responseOutput.ReplaceAll("(</?)(\w+:)","$1") />
         <cfset responseOutput = REReplaceNoCase(responseOutput,"<feed[^>]*>","<feed>") />
         <!---entry nodes hold the data--->
         <cfset entryNodes = XmlSearch(responseOutput, '//entry/') />

         <cfreturn entryNodes />
    </cffunction>

Next, a list of the profiles (websites) that the user has access to are retrieved by requesting an account feed. If the user has only one profile associated with their credentials, the data is displayed immediately. If they have more than one, they are presented with a select box to select the profile they want data from.

<cfinvoke component="ga" method="parseProfiles" returnvariable="profilesArray"></cfinvoke>
 <cffunction name="parseProfiles" access="public" returntype="array" hint="GA profiles as array of structures">

	<cfset var profileArray = ArrayNew(1) />
        <cfset var entryStruct = StructNew() />

        <cfset entryNodes = callApi("https://www.google.com/analytics/feeds/accounts/default") />

        <cfloop array="#entryNodes#" index="entry">
            <cfset entryStruct = StructNew() />

            <cfset entryStruct.title = entry.title.XmlText />
            <cfset entryStruct.tableId = entry.tableId.XmlText />

            <cfset arrayAppend(profileArray,duplicate(entryStruct)) />
        </cfloop>

        <cfreturn profileArray />
    </cffunction>

A default date range of one year is set initially. A dialog box that allows the user to change the date range is presented as an option. The date range is limited to a max date of yesterday because GA does not have full data for the current day. This causes the averages that are calculated on the return data to be skewed.

A data feed request url is sent to GA along with the auth token a via cfhttp tag.

<cfset reqUrl = "https://www.google.com/analytics/feeds/data?ids=" & session.tableId & "&metrics=ga:newVisits,ga:pageviews,ga:visits,ga:visitors,ga:timeOnSite&start-date=" & session.startdate & "&end-date=" & session.enddate />

The XML response is then parsed out and returned as an array of structures.

<cffunction name="parseData" access="public" hint="GA data as array of structures set in session">
        <cfargument name="gaUrl" type="string" required="yes" />
        <cfargument name="arrayName" type="string"required="yes" />

        <cfset var dataArray = ArrayNew(1) />
        <cfset var entryStruct = StructNew() />

         <cfset entryNodes = callApi(arguments.gaUrl) />

        <!---CF8 loop through the entries and put each data point in structure--->
        <cfloop from="1" to="#ArrayLen(entryNodes)#" index="num">
		<!---rest of the stats data from GA, first check if dimension exists--->
		<cfif StructKeyExists(entryNodes[num],"dimension")>
		<cfloop from="1" to="#ArrayLen(entryNodes[num].dimension)#" index="i">
		<!---start after ga: to remove it--->
		<cfset "entryStruct.#Mid(entryNodes[num].dimension[i].XmlAttributes["name"],4,
Len(entryNodes[num].dimension[i].XmlAttributes["name"]))#" = entryNodes[num].dimension[i].XmlAttributes["value"] />
		</cfloop>
		</cfif>

		<cfloop from="1" to="#ArrayLen(entryNodes[num].metric)#" index="i">
			<cfset "entryStruct.#Mid(entryNodes[num].metric[i].XmlAttributes["name"],4,
Len(entryNodes[num].metric[i].XmlAttributes["name"]))#" = entryNodes[num].metric[i].XmlAttributes["value"] />
		</cfloop>

		<cfset arrayAppend(dataArray,duplicate(entryStruct)) />

	</cfloop>

         <!---CF9 can use this simplier loop. If you have CF9, uncomment and delete loop above
            <cfloop array="#entryNodes#" index="entry">
		<cfif StructKeyExists(entry,"dimension")>
                	<cfloop array="#entry.dimension#" index="dimension">
                 	<cfset "entryStruct.#Mid(dimension.XmlAttributes["name"],4,
Len(dimension.XmlAttributes["name"]))#" = dimension.XmlAttributes["value"] />
                 	</cfloop>
                 </cfif>

                 <cfloop array="#entry.metric#" index="metric">
                 	<cfset "entryStruct.#Mid(metric.XmlAttributes["name"],4,
Len(metric.XmlAttributes["name"]))#" = metric.XmlAttributes["value"] />
                  </cfloop>

                <cfset arrayAppend(dataArray,duplicate(entryStruct)) />
           </cfloop>--->

        	<cflock scope="session" type="exclusive" timeout="5">
        		<cfset "session.#arrayName#" = dataArray />
        	</cflock>

    </cffunction>

Once the data is in a form you can manipulate, graphs, tables, and charts of all kinds can be made.

ClientLogin, AuthSub, and Secure AuthSub authentication examples are included in the demo. ClientLogin recommended only for client apps. Google notice on using ClientLogin:

“Important: Do not use ClientLogin if you are writing an application that runs on
your computer to make requests on behalf of 3rd party end users. Instead, use either
AuthSub or OAuth, which protects end users’ private data. Because ClientLogin stores
the user login data, it should only be used in cases where that data is under the
direct control of the user (e.g. their personal computer).”

Recommended Reading

Download files from github

Passed the Google Analytics Individual Qualification Test

I took and passed the Google Analytics Individual Qualification test today. It wasn’t too bad but it did require studying unless you dead set on cheating. I studied all the presentations on Google’s Conversion University, running through the more difficult stuff twice (AdWords, e-commerce).

It costs $50 to take. Seems a little steep but I did learn so much more than I would have had I not been so motivated. Meaning: I wasn’t going to waste my $50.

I passed!

Recommended Reading

Google Analytics API Login Authentication with ColdFusion

You might also like the ColdFusion and Google Analytics: Getting Out What You Put In post.

Updated 08-02-2009: Replaced this line:

<cfset accountXML = callApi("https://www.google.com/analytics/feeds/accounts/#urlEncodedFormat(form.Email)#",loginAuth) />

with this:

<cfset accountXML = callApi("https://www.google.com/analytics/feeds/accounts/default",loginAuth) />

My Hooking into Google Analytics with ColdFusion post received a suggestion from Raymond Camden that authentication and token acquisition could be done via cfhttp for access to Google Analytics information. So, here it is again with authentication built in. I set this up with a email/password form but you could easily hard code the email and password in if you so desired.

1. Prompt for email and password of Google account that has access to Google Analytics data. This is an all-in-one example so the form submits back to its own page and processing continues.

<form action="GAwithCFlogin.cfm" method="post">
Google email <input type="text" name="Email" /><br />
Password <input type="password" name="password" /><br />
<input type="submit" />
</form>

2. Authenticate the credentials with Google.

<cfset loginAuth = googleLogin(form.Email,form.password) />

The googleLogin function sends the email/password combo along with the service and source values as URL parameters required by Google to Google’s client login URL via a post request. The source parameter is a Short string identifying your application, for logging purposes. This string should take the form: “companyName-applicationName-versionID” according to Google. You can put in your company name or whatever name you want followed by the rest of the convention Google suggests. Example: yourcompanyname-analytics-1.0. The default start and end dates are also set here.

<!---dates for one full year of stats (default)--->
<cfset startdate = DateFormat(DateAdd("d",-365,CreateDate(Year(Now()),Month(Now()),Day(Now()))), "yyyy-mm-dd") />
<cfset enddate = DateFormat(DateAdd("d", -1, Now()),"yyyy-mm-dd") />

<cffunction name="googleLogin" access="public" returntype="string">
	<cfargument name="email" type="string" required="yes" default="">
	<cfargument name="password" type="string"required="yes" default="">
    <cfargument name="gaLoginUrl" type="string" required="no" default="https://www.google.com/accounts/ClientLogin">

    <cfset var loginAuth = "" />

    <cfhttp url="#arguments.gaLoginUrl#" method="post">
        <cfhttpparam name="accountType" type="url" value="GOOGLE">
        <cfhttpparam name="Email" type="url" value="#arguments.email#">
        <cfhttpparam name="Passwd" type="url" value="#arguments.password#">
        <cfhttpparam name="service" type="url" value="analytics">
        <cfhttpparam name="source" type="url" value="yourCompanyName-analytics-1.0">
    </cfhttp>

	<cfif NOT FindNoCase("Auth=",cfhttp.filecontent)>
		<cfset loginAuth = "Authorization Failed" />
	<cfelse>
		<cfset loginAuth = Mid(cfhttp.filecontent, FindNoCase("Auth=",cfhttp.filecontent) + (Len("Auth=")), Len(cfhttp.filecontent)) />
	</cfif>

    <cfreturn loginAuth />
</cffunction>

What should come back is a long string of authorization token values. There are several but we only need the Auth token. If it’s not in there, we send back “Authorization Failed.” If it’s in there, we strip it out and return it.

3. Get the accounts associated with the email. There can be more than one set up in Analytics.

<cfset accountXML = callApi("https://www.google.com/analytics/feeds/accounts/default",loginAuth) />

The email account is appended onto the Google URL and a get call is made from the callApi function with the loginAuth token attached as a header value.

<cffunction name="callApi" access="public" returntype="string">
    <cfargument name="gaUrl" type="string" required="yes">
    <cfargument name="authToken" type="string" required="yes">

    <cfset var authSubToken = 'GoogleLogin auth=' & arguments.authToken />
    <cfset var responseOutput = "" />

    <cfhttp url="#arguments.gaUrl#" method="get">
        <cfhttpparam name="Authorization" type="header" value="#authSubToken#">
    </cfhttp>

    <cfset responseOutput = cfhttp.filecontent />

    <cfreturn responseOutput />
</cffunction>

The header value must be in the form of Authentication: GoogleLogin auth=DF3843483…(it’s kind of long but you get the idea). This authentication is different form AuthSub Proxy authentication used in my previous post.

4. Clean up the XML that returned so we can use XmlSearch() and other handy functions available in ColdFusion. For some reason the atom specification of the XML file bungles those functions up. We need to remove the xmlns nodes from the feed element and remove the dxp prefixes. Do a cfdump on the XML before and after two lines of code used to tidy them up to seed what happens.

<cfdump var="#accountXML#">

Then roll through the accountXML with to grab all the profiles and toss them into an array so we can loop through them later.

<!---remove dxp: prefix from nodes that have it--->
	<cfset accountXML = accountXML.ReplaceAll("(</?)(\w+:)","$1") />
	<!---Strip xmlns from feed element--->
	<cfset accountXML = REReplaceNoCase(accountXML,"<feed[^>]*>","<feed>") />

	<cfset entryNodes = XmlSearch(accountXML, '//entry/') />

	<cfset profileArray = ArrayNew(1) />

	<cfloop from="1" to="#ArrayLen(entryNodes)#" index="num">
		<cfset entryStruct = StructNew() />

		<cfset entryStruct.id = entryNodes[num].id.XmlText />
		<cfset entryStruct.title = entryNodes[num].title.XmlText />
		<cfset entryStruct.tableId = entryNodes[num].tableId.XmlText />

		<cfloop from="1" to="#ArrayLen(entryNodes[num].property)#" index="i">
   			<cfswitch expression='#entryNodes[num].property[i].XmlAttributes["name"]#'>
    		    <cfcase value="ga:accountId">
       		     <cfset entryStruct.accountId = entryNodes[num].property[i].XmlAttributes["value"] />
     		   </cfcase>
      		  <cfcase value="ga:accountName">
    		        <cfset entryStruct.accountName = entryNodes[num].property[i].XmlAttributes["value"] />
 		       </cfcase>
		         <cfcase value="ga:profileId">
		               <cfset entryStruct.profileId = entryNodes[num].property[i].XmlAttributes["value"] />
	            </cfcase>
	            <cfcase value="ga:webPropertyId">
	                <cfset entryStruct.webPropertyId = entryNodes[num].property[i].XmlAttributes["value"] />
	            </cfcase>
	        </cfswitch>
	    </cfloop>

		    <cfset arrayAppend(profileArray,duplicate(entryStruct)) />

		</cfloop>

5. Loop through the profiles, grab the page views and visitors, and tally them up for all profiles.

<cfset totalpageviews = 0 />
<cfset totalvisitors = 0 />

		<cfloop from="1" to="#ArrayLen(profileArray)#" index="num">
	    	<cfset reqUrl = "https://www.google.com/analytics/feeds/data?ids=" & profileArray[num].tableId & "&metrics=ga:pageviews,ga:visitors&start-date=" & startdate & "&end-date=" & enddate />

	 		 <cfset statsXML = callApi(reqUrl,loginAuth) />
	                <!---Tidy up the XML again --->
	 		 <cfset statsXML = statsXML.ReplaceAll("(</?)(\w+:)","$1") />
			 <cfset statsXML = REReplaceNoCase(statsXML,"<feed[^>]*>","<feed>") />

			 <cfset metric = XmlSearch(statsXML, '//metric/') />

			 <cfset pageviews = metric[1].XmlAttributes["value"] />

                         <cfset visitors = metric[2].XmlAttributes["value"] />

			 <cfset totalpageviews = totalpageviews + pageviews />

                         <cfset totalvisitors = totalvisitors + visitors />

			 <p><strong>Profile:</strong> <em>#profileArray[num].title#</em> Pageviews: #NumberFormat(pageviews, ",")# Visitors: #NumberFormat(visitors, ",")# </p>

		 </cfloop>

		<p><strong>Total pageviews:</strong> #NumberFormat(totalpageviews, ",")#</p>
                <p><strong>Total visitors:</strong> #NumberFormat(totalvisitors, ",")#</p>

All done. Thank you Raymond Camden and Alex Curelea. Alex got me pointed in the right direction with his post Using the Google Analytics API – getting total number of page views. And, as I mentioned at the get go, Raymond Camden made the suggestion of using the login as some of his examples have done in the past.

Recommended Reading

Download

Information on Google’s Authentication for Installed Application ClientLogin and the Google Analytics Data API ClientLogin Username/Password Authentication.

Helpful ColdFusion books include the all-in-one ColdFusion MX 7 WACK with tons of good information even if you’ve moved up to CF8. Crazy as it seems, Adobe decided to break up ColdFusion 8 books into three volumes: Volume 1: Getting Started, Volume 2: Application Development, and Volume 3: Advanced Application Development. You may be able to pick up a used copy in good condition. Follow the book link to Amazon and look for used copies underneath the books title.

Hooking into Google Analytics with ColdFusion

You might also like the ColdFusion and Google Analytics: Getting Out What You Put In post.

Using Alex Curelea’s example of using PHP to hook into Google Analytics through their API, I developed a ColdFusion method. So, same thing as Alex just flavored with CF. The code grabs the profiles and pageviews, then totals up the pageviews for all profiles.

Update: I followed Raymond Camden’s suggestion below and posted another version of this using ClientLogin Authentication.

1. Authenticate user and get token from Google. Basically just a link that sends them off to Google and Google returns them to your page designated in the href with token attached as URL param.

<a href="https://www.google.com/accounts/AuthSubRequest?next=http://www.your-site-here.com/GAwithCF.cfm&scope=https://www.google.com/analytics/feeds/
&secure=0&session=1">Authenticate through Google.</a>

2. Send that token back to Google and get a session token that can be used to retrieve the profile information and data. This is handled in two functions that I originally had in a CFC. I put all the code on one page for this example.

For the session token required for access to the data, the sessionToken function is called with the URL.token passed in.

<cfset authSessionToken = sessionToken(URL.token) />

The sessionToken function makes a get request to https://www.google.com/accounts/AuthSubSessionToken with the token as a header value using the callApi function. The header value must be in the form of Authorization: AuthSub token=”C8b_xxxxxxxxx” with the token equaling the URL.token.
The session token comes back in the form of “Token=CH____88dfdsfsd.” Strip off the “Token=” and you have your session token. The default start and end dates are also set here.

<!---dates for one full year of stats (default)--->
<cfset startdate = DateFormat(DateAdd("d",-365,CreateDate(Year(Now()),Month(Now()),Day(Now()))), "yyyy-mm-dd") />
<cfset enddate = DateFormat(DateAdd("d", -1, Now()),"yyyy-mm-dd") />

<cffunction name="sessionToken" access="public" returntype="string">
	<cfargument name="authToken" type="string" required="yes">

    <cfset var output = callApi("https://www.google.com/accounts/AuthSubSessionToken",arguments.authToken) />
    <cfset var authSubSessionToken = "" />

    <cfset authSubSessionToken = Mid(output, FindNoCase("Token=",output) + (Len("Token=")), Len(output)) />

    <cfreturn authSubSessionToken />
</cffunction>

<cffunction name="callApi" access="public" returntype="string">
    <cfargument name="gaUrl" type="string" required="yes">
    <cfargument name="authToken" type="string" required="yes">

    <cfset var authSubToken = 'AuthSub token="' & arguments.authToken & '"' />
    <cfset var responseOutput = "" />

    <cfhttp url="#arguments.gaUrl#" method="get">
        <cfhttpparam name="Authorization" type="header" value="#authSubToken#">
    </cfhttp>

    <cfset responseOutput = cfhttp.filecontent />

    <cfreturn responseOutput />
</cffunction>

3. Now the callApi function can be used with the authSessionToken to return the account profiles as XML. Unfortunately, it’s an atom feed XML so a little stripping must occur before we can use the handy XMLSearch function in ColdFusion.

<cfset accountXML = callApi("https://www.google.com/analytics/feeds/accounts/default",authSessionToken) />

First we remove the nodes with a dxp: prefix. Then we clear the “xmlns” nodes in the feed element. That’s what bugs up XMLSearch.

<cfset accountXML = accountXML.ReplaceAll("(</?)(\w+:)","$1") />
<cfset accountXML = REReplaceNoCase(accountXML,"<feed[^>]*>","<feed>") />

Anytime you want to take a peek at the XML, you can use cfdump to spit it out for you.

<cfdump var="#XMLParse(accountXML)#">

4. Parse out the XML to put the profiles in an array for easy looping later.

<cfset entryNodes = XmlSearch(accountXML, '//entry/') />

<cfset profileArray = ArrayNew(1) />

<cfloop from="1" to="#ArrayLen(entryNodes)#" index="num">
    <cfset entryStruct = StructNew() />

    <cfset entryStruct.id = entryNodes[num].id.XmlText />
    <cfset entryStruct.title = entryNodes[num].title.XmlText />
    <cfset entryStruct.tableId = entryNodes[num].tableId.XmlText />

    <cfloop from="1" to="#ArrayLen(entryNodes[num].property)#" index="i">
        <cfswitch expression='#entryNodes[num].property[i].XmlAttributes["name"]#'>
            <cfcase value="ga:accountId">
                <cfset entryStruct.accountId = entryNodes[num].property[i].XmlAttributes["value"] />
            </cfcase>
            <cfcase value="ga:accountName">
                <cfset entryStruct.accountName = entryNodes[num].property[i].XmlAttributes["value"] />
            </cfcase>
            <cfcase value="ga:profileId">
                <cfset entryStruct.profileId = entryNodes[num].property[i].XmlAttributes["value"] />
            </cfcase>
            <cfcase value="ga:webPropertyId">
                <cfset entryStruct.webPropertyId = entryNodes[num].property[i].XmlAttributes["value"] />
            </cfcase>
        </cfswitch>
    </cfloop>

    <cfset arrayAppend(profileArray,duplicate(entryStruct)) />

</cfloop>

5. Loop through each profile, grab the metrics you want for each profile from Google, display them on the page, and tally up the total page views count. The XML needs some tidying again but you saw that earlier.

<cfset totalpageviews = 0 />

<cfloop from="1" to="#ArrayLen(profileArray)#" index="num">
    <cfset reqUrl = "https://www.google.com/analytics/feeds/data?ids=" & profileArray[num].tableId & "&metrics=ga:pageviews&start-date=" & startdate & "&end-date=" & enddate />

    <cfset statsXML = callApi(reqUrl,authSessionToken) />
    <!---Tidy up the XML--->
    <cfset statsXML = statsXML.ReplaceAll("(</?)(\w+:)","$1") />
    <cfset statsXML = REReplaceNoCase(statsXML,"<feed[^>]*>","<feed>") />

<cfset metric = XmlSearch(statsXML, '//metric/') />

<cfset pageviews = metric[1].XmlAttributes["value"] />

<cfset totalpageviews = totalpageviews + pageviews />

<p>Profile: #profileArray[num].title# #NumberFormat(pageviews, ",")#</p>

</cfloop>

<p>Total pageviews: #NumberFormat(totalpageviews, ",")#</p>

That’s it. It should at least get you started. Thanks Alex!

Recommended Reading

Download

News of the Google Analytics API

Information on Google AuthSub Proxy Authentication.

Helpful ColdFusion books include the all-in-one ColdFusion MX 7 WACK with tons of good information even if you’ve moved up to CF8. Crazy as it seems, Adobe decided to break up ColdFusion 8 books into three volumes: Volume 1: Getting Started, Volume 2: Application Development, and Volume 3: Advanced Application Development. You may be able to pick up a used copy in good condition. Follow the book’s link above to Amazon and look for used copies underneath the books title.