26Aug

Google Analytics and AIR

1 comment so far

So I thought I would take my old post of finding the median value of my Google Analytics and convert this to an Adobe AIR application. Here is the old post.

Here is the code I am beginning with:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="800" height="800">
<mx:Script>
<![CDATA[
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.filesystem.File;
import mx.controls.Alert;
import MedianGraph;

private function loadGA():void
{
if ( !loc.text || loc.text == "Enter Location of XML File")
{
Alert.show ("Please enter location of your Google Analytics file", "ERROR");
}
else
{
// read the file
var myFile:File = File.documentsDirectory.resolvePath( loc.text );
var fileStream:FileStream = new FileStream();
fileStream.open(myFile, FileMode.READ);

var xml:XML = new XML(fileStream.readUTFBytes(fileStream.bytesAvailable));

var pointList:XMLList = xml.Report.Graph[0].Serie.Point.Value.text();
var timeList:XMLList = xml.Report.Graph[0].Serie.Point.Label.text();

var xPoint:Array = new Array;

for each (var graphPoints:XML in pointList)
{
xPoint.push( graphPoints );
}

var tPoint:Array = new Array;

for each (var timePoints:XML in timeList)
{
tPoint.push ( timePoints );
}

var graphArray:Array = new Array;
for (var c:uint; c < xPoint.length; c++){
graphArray.push ({point:xPoint[c], time:tPoint[c]})
}

// sort the array from lowest to highest
var medianArray:Array = new Array();
medianArray = graphArray.sortOn("point", Array.NUMERIC);

// check to see if there is an even or odd numbers in the list
var evenOdd:uint = medianArray.length%2;
var medianArrIndex:Number;

if ( evenOdd == 1 )
{
/* odd number */
/* take the middle value */
medianArrIndex = medianArray[Math.floor(medianArray.length / 2)].point;
}
else
{
/* even number */
/* take the 2 middle numbers, add them together and divide by 2 */
var middle:Number = medianArray.length / 2;

var lowMiddle:Number = medianArray[middle - 1].point;
var highMiddle:Number = medianArray[middle].point;
medianArrIndex = (highMiddle + lowMiddle) / 2;

var medianPoint:Array = new Array();

/* ——————————————- */
for (var x:uint; x < medianArray.length; x++)
{
medianPoint.push ({point:medianArrIndex, time:tPoint[x]})
}
/* ——————————————- */
}

Alert.show ("The median point is: " + medianArrIndex);
fileStream.close();
}
}

private function browseForOpen():void
{
var file:File = new File();
file.addEventListener(Event.SELECT, openHandler);
file.browseForOpen("Select a File");
}

private function openHandler(e:Event):void
{
loc.text = e.target.nativePath;
}
]]>
</mx:Script>
<mx:TextInput text="Enter Location of XML File" width="500" x="10" y="3" id="loc" enter="loadGA()" />
<mx:Button click="browseForOpen()" label="Browse for File" x="511" y="3" />
<mx:Button click="loadGA()" label="GO!" x="650" y="3" />
</mx:WindowedApplication>

Share/Save/Bookmark

Categories: AIR, Actionscript 3

Tuesday, August 26th, 2008 at 9:36 am and is filed under AIR, Actionscript 3. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Google Analytics and AIR”

  1. Posted by Bookmarks about Analytics 29th October, 2008 at 12:30 pm

    [...] - bookmarked by 6 members originally found by ct5821 on 2008-10-09 Google Analytics and AIR http://manewc.com/2008/08/26/google-analytics-and-air/ - bookmarked by 1 members originally found [...]

Leave a reply