Open Source ActionScript 3 Google Analytics API

This post is also available in: Английский

We have published sources and all information about recently developed AS3 Google Analytics API. We would like to see you uncovering potential of Google Analytics in your Flash and AIR applications. It is in the early stage of development but already gives you powerful tools of access to analytics data in simple and flexible form. We will continue to develop AS3 Google Analytics API and add new features, that’s why we would like to get your feedback and feature requests. Visit project page.

Using AS3 Google Analytics API is pretty simple as several lines of code. First of all import swc library into your project and all needed classes in your package.

Download
Download latest version here. It could be useful to download sources as well.

Requesting data feed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var apiloader_ga:APILoader = new APILoader();
var gav:GAView = new VisitorsView();
// Configure your google account profile id
apiloader_ga.setProfileID("ga:profilenum");
// Configure dimensions to request from GA
apiloader_ga.setDimensions(new Array(new VisitorDateDimension()));
// Configure metrics to request from GA
apiloader_ga.setMetrics(new Array(new VisitorVisitsMetric() ));
// Configure request filter
apiloader_ga.setFilter(new Filter(new FilterItem(new VisitorCountryDimension(), new FilterOperator("=="),"Russia")));
// Analytics data timeframe setup
var start_date:Date = new Date(2010, 10, 9);
var end_date:Date = new Date(2010, 11, 8);
apiloader_ga.setPeriod(start_date, end_date);
               
// Set callback function (returns array of DataFeedResponse objects)
apiloader_ga.setCallback(refreshGraph);
               
// Configure view (process data and prepares everything in your way)
// You could pass any class implementing GAView or choose from already developed
apiloader_ga.registerView(gav);

// Connect and begin data loading
apiloader_ga.connect(new GAAccount("yourgmail@gmail.com", "yourpassword"));

Retrieving results
You could get results in two ways.
First one is by using callback function where you get an array of DataFeedResponse objects

1
2
3
function refreshGraph(a:Array):void {
// parse an array of DataFeedResponse objects
}

Second approach is in providing GAView descendant object to APILoader object. APILoader during consequent pages loading will be filling GAView object with data. You may check sample implementation in com.denivip.ga.view.VisitorsView class.

Sample
In order to quickly integrate Google Analytics API into your Flash application please check our demo app source codes. It shows visitors from Russia on your site (you should compile with your profile id and account credentials). Sources.