Google Analytics Charts with Lasso
Parse and display Google Analytics data using the Analytics Data API, Lasso and a Flash plugin
This example uses a Flash plugin (amCharts) to display the graph, but nicer options are now available wihtout Flash, for example: http://www.filamentgroup.com/lab/jquery_visualize_plugin_accessible_charts_graphs_from_tables_html5_canvas/
Here you can see what all this code will output for your site :http://www.quinode.fr/lasso/ga.png
It uses the clientlogin auth methode from Google, which is the simplest one...
It could be extended as a tag by letting choose the ga:dimensions dynamically instead of repeating the requests as I did, so you could chose which data yo want to show. For now, I included most of the info you find in the GA dasboard when you login
Caution it uses probably some ctags from tagswap as xml_tree, date_firstdayofweek, so you'll have some errors until you get them all...
This whole page is designed to be loaded dynamically through an AJAX tab using jQuery UI tabs (see further), to dynamically pass the main parameter : timerange. By default, the first tab is loaded and display the "month" time range
local('gwt_id')='xxxxxxxxxx'; //the ID from Google for this site
// You can find this ID when looking at your Analytics home page : in the link "Show this report", it's the 'id'
param
var('timerange') = if_empty(client_param('timerange'),'month');
if($timerange == 'month');
var('end') = date_format(Date_Subtract(server_date,-day=1),-format='%Q');
var('start') = date_format(Date_Subtract(server_date,-month=1),-format='%Q');
var('dimensions') = 'ga:day,ga:month';
var('sorting')= 'ga:monthr,ga:day';
else if($timerange == 'sixmonth');
var('start') = date_format(Date_Subtract(server_date,-month=6),-format='%Y-%m-01');
var('end') = date_add($start,-month=5);
var('end') = Date_Format($end,-format='%Y-%m-'lp_date_monthlastday(-month=Date_GetMonth($end),
-year=Date_GetYear($end)),-format='%Q');
var('dimensions') = 'ga:week,ga:year';
var('sorting')= 'ga:year,ga:week';
else if($timerange == 'year');
var('start') = date_format(Date_Subtract(server_date,-month=13),-format='%Y-%m-01');
var('end') = date_add($start,-year=1);
var('end') = Date_Format($end,-format='%Y-%m-'lp_date_monthlastday(-month=Date_GetMonth($end),
-year=Date_GetYear($end)),-format='%Q');
var('dimensions') = 'ga:month,ga:year';
var('sorting')= 'ga:year,ga:month';
/if;
local('prms') = array(
'accountType' = 'GOOGLE',
'Email' = 'xxxxxxxxxxxxxxxx@gmail.com', // your Google account
'Passwd' = 'xxxxxxxxxxx',
'service' = 'analytics',
'source'='YourCMSname' //just for info
);
protect;
local('auth') = include_url('https://www.google.com/accounts/ClientLogin',
-postparams=#prms,
-timeout=15
);
iterate($auth->split('n'),local('a'));
#a!='' && #a->split('=')->first == 'Auth' ?
var('googleAuth') = #a;
/iterate;
protect;
local('visits_feed') = include_url('https://www.google.com/analytics/feeds/data',
-getparams=array( 'ids'='ga:'#gwt_id,
'dimensions'=$dimensions,
'metrics'='ga:visits,ga:bounces,ga:entrances,ga:pageviews,ga:timeOnSite',
'sort'=$sorting,
'start-date'=$start,
'end-date'=$end,
//'start-index'='10',
//'max-results'='100',
),
-SendMIMEHeaders=array('Authorization' = 'GoogleLogin '$googleAuth)
);
local('browser_feed') = include_url('https://www.google.com/analytics/feeds/data',
-getparams=array( 'ids'='ga:'#gwt_id,
'dimensions'='ga:browser',
'metrics'='ga:visits',
'sort'='-ga:visits',
'start-date'=$start,
'end-date'=$end,
//'start-index'='10',
'max-results'='10',
),
-SendMIMEHeaders=array('Authorization' = 'GoogleLogin '$googleAuth)
);
local('geo_feed') = include_url('https://www.google.com/analytics/feeds/data',
-getparams=array( 'ids'='ga:'#gwt_id,
'dimensions'='ga:country',
'metrics'='ga:visits',
'sort'='-ga:visits',
'start-date'=$start,
'end-date'=$end,
//'start-index'='10',
'max-results'='10',
),
-SendMIMEHeaders=array('Authorization' = 'GoogleLogin '$googleAuth)
);
protect;
var('visits_xml') = xml_tree($visits_feed);
var('browser_xml') = xml_tree($browser_feed);
var('geo_xml') = xml_tree($geo_feed);
var('entries') = $visits_xml->entry;
var( 'data' =array(),
'slices'=array(),
'browsers'= array(),
'countries'=array(),
'visits'= integer(0),
'bounces'= integer(0),
'entrances'= integer(0),
'pageviews'= integer(0),
'time' = integer(0)
);
iterate($entries,local('e'));
local('val') = integer(#e->metric(1)->attribute('value'));
$data->insert(#val);
$visits += #val;
$bounces += integer(#e->metric(2)->attribute('value'));
$entrances += integer(#e->metric(3)->attribute('value'));
$pageviews += integer(#e->metric(4)->attribute('value'));
$time += decimal(#e->metric(5)->attribute('value'));
/iterate;
if($visits!='0');
if($bounces!='0' && $entrances!='0');
var('bouncerate') = decimal(decimal($bounces)/decimal($entrances)*100);
$bouncerate->setformat(-precision=2);
/if;
if($pageviews!='0' && $visits!='0');
var('pages') = decimal(decimal($pageviews)/decimal($visits));
$pages->setformat(-precision=2);
/if;
if($time!='0' && $visits!='0');
var('alltime') = integer(decimal($time)/decimal($visits));
var('timemoy') = date_add(date('2000-01-01 00:00:00'),-second=$alltime);
/if;
iterate($browser_xml->entry,local('b'));
local('btx') = decimal(decimal(#b->metric->attribute('value'))/decimal($visits)*100.00);
#btx->setformat(-precision=2);
$browsers->insert(#b->dimension->attribute('value') = #btx);
/iterate;
iterate($geo_xml->entry,local('g'));
local('gtx') = decimal(decimal(#g->metric->attribute('value'))/decimal($visits)*100.00);
#gtx->setformat(-precision=2);
$countries->insert(array( #g->dimension->attribute('value'),
#g->metric->attribute('value'),
#gtx));
/iterate;
/if;
if($timerange == 'month');
$slices->insert(datetime($start, -language='fr', -setformat='%A %d %B') =
Date_GetDay($start)); //first day
loop($entries->size);
local('day') = Date_Add($start,-day=loop_count);
$temps->insert(datetime(#day, -language='fr', -setformat='%A %d %B') =
Date_GetDay(#day));
//Using french format here
/loop;
$slices->removelast;
else if($timerange == 'sixmonth');
local('week') = integer(jina_weeknum($start)); //first week
local('fdoweek') = date_firstdayofweek(-date=date($start));
$slices->insert('Week '#week' from 'date_format(#fdoweek,-format='%d/%m/%y')' to
'date_format(date_add(#fdoweek,-day=6),-format='%d/%m/%y') = #week);
loop($entries->size);
#week += 1;
#fdoweek = date_Add(#fdoweek,-day=7);
$slices->insert('Semaine '#week' du 'date_format(#fdoweek,-format='%d/%m/%y')'
au 'date_format(date_add(#fdoweek,-day=6),-format='%d/%m/%y'
) = #week);
/loop;
$slices->removelast;
else if($timerange == 'annee');
//local('month1') = Date_GetMonth($start); //first month mois
local('month') = string(datetime($start, -language='fr', -setformat='%B'));
$slices->insert(#month = #month);
loop($entries->size);
local('month') = string(datetime(Date_add($start,-month=loop_count),
-language='fr', -setformat='%B'));
$temps->insert(#month = #month);
/loop;
$slices->removelast;
/if;
//Here starts the formatting specific to the Flash plugin
var('amxml') = string('<chart><series>');
iterate($slices,local('d'));
$amxml += '<value xid="'loop_count'">'#d->value'</value>';
/iterate;
$amxml += '</series><graphs>';
$amxml += '<graph gid="'0'" title="Visits within this time range">';
iterate($data,local('d'));
$amxml += '<value description="'$slices->get(loop_count)->name'" xid="'loop_count'">'#d'</value>';
/iterate;
$amxml +='</graph></graphs></chart>';
handle;
error_code!='0'?'XML parsing : 'error_msg;
/handle;
/protect;
handle;
error_code!='0'?'Feed connection : 'error_msg;
/handle;
/protect;
handle;
error_code!='0'?'Auth connection : 'error_msg;
/handle;
/protect;
var('range_links') = (: 'This month' = 'month',
'Last 6 monthes' = 'sixmonth',
'This year' = 'year');
Here are the jQuery tabs, to reload the page using a different time range.
<?lassoscript
'<ul class="statlinks"> ';
iterate($range_links,local('l'));
#l->value == $timerange ?
'<li class="selected">'#l->name'</li>n'|
'<li class="link"><a href="/ajax/stats.html?timerange='#l->value'">'#l->name'</a></li>n';
/iterate;
'</ul>';
?>
Finally, the Flash plugin to display the data, you'll find the needed files at http://www.amcharts.com/
<div id="ct_amchart">
<!-- amline script-->
<script type="text/javascript" src="/yourpath/amline/swfobject.js"></script>
<div id="amchart1">
<strong>You need to upgrade your Flash Player</strong>
</div>
<script type="text/javascript">
var so = new SWFObject('/yourpath/amline/amline.swf', 'amline', '600', '300', '8', '#FFFFFF');
so.addVariable('path', '/cms_inc/amline/');
so.addVariable('chart_data', encodeURIComponent('[$amxml]'));
so.addVariable('chart_settings', encodeURIComponent('<settings><background><color>#daf2ff,#FFFFFF</color>
<alpha>100</alpha></background><labels><label><y>24</y><width>520</width><align>center</align>
<text>['<![CDATA[']<b><br/>Visits from [date_format($start,-format='%d/%m/%y')] to [date_format($end,
-format='%d/%m/%y')] ([
select($timerange);
case('month');'by day';
case('semestre');'by week';
case('annee');'by month';
/select
])</b>[']]>']</text></label></labels></settings>'));
so.write('amchart1');
</script>
<!-- end of amline script -->
</div>
Then we display the other aggregated data, mimicing the most importants data the Google Analytics show by default.
<div class="statnote">
<span>[$visits_xml->aggregates->metric(1)->attribute('value')]</span> visits on this time range.<br/>
<span>[$bouncerate]%</span> Bounces.<br/>
<span>[$pages]</span> Page Views.<br/>
<span>[date_format($timemoy,-format='%T')]</span> Time on the site.</span>
</div>
Use your own CSS to make the div.statnote > span
looks nice...
local('wordsfeed') =
include_url('https://www.google.com/analytics/feeds/data', -getparams=array(
'ids'='ga:'#gwt_id,
'dimensions'='ga:keyword',
'metrics'='ga:visits',
'sort'='-ga:visits',
'filters'='ga:medium==organic',
'start-date'=$start,
'end-date'=$end,
'start-index'='1',
'max-results'='50',
),
-SendMIMEHeaders=array('Authorization' = 'GoogleLogin '+$googleAuth));
var('wordsxml') = xml_tree($wordsfeed);
var('keywords') = $wordsxml->entry;
'<div class="statnote"><span>';
$wordsxml->aggregates->metric->attribute('value');
'</span> visitors came from a search engine, using <br/><span>'+$wordsxml->totalResults->contents+'</span>
differents keywords.</div>';
'<fieldset class="keyset"><legend><b>50 most used keywords.</b></legend>';
'<table class="statable" id="kwrds" border="0" cellspacing="0" cellpadding="4">';
iterate($keywords,local('k'));
local('thisk') = #k->dimension->attribute('value');
if(#thisk != '(not set)');
'<tr class="'(loop_count%2=='0'?'odd'|'even')'"><td><a href="http://www.google.fr/#hl=fr&q='+#thisk+'"
target="_blank">'#thisk'</a></td><td align="right">'+#k->metric->attribute('value')+'</td></tr>';
/if;
/iterate;
'</table></fieldset>';
'<fieldset class="colset"><legend><b>Geographical</b></legend>
<table class="statable" id="geovsts" border="0" cellspacing="0" cellpadding="4">';
iterate($countries,local('g'));
'<tr class="'(loop_count%2=='0'?'odd'|'even')'"><td>'#g->first'</td><td align="right">'+#g->get(3)+'%</td>
<td align="right">'#g->get(2)'</td>
</tr>';
/iterate;
'</table></fieldset><br/>';
'<fieldset class="colset"><legend><b>Browsers</b></legend>
<table class="statable" id="brwsrs" border="0" cellspacing="0" cellpadding="4">';
iterate($browsers,local('b'));
'<tr class="'(loop_count%2=='0'?'odd'|'even')'"><td>'#b->name'</td><td align="right">'#b->value'%</td></tr>';
/iterate;
'</table></fieldset>';
Author: Dominique Guardiola Falco
Created: 26 Apr 2010
Last Modified: 16 Mar 2011
Please note that periodically LassoSoft will go through the notes and may incorporate information from them into the documentation. Any submission here gives LassoSoft a non-exclusive license and will be made available in various formats to the Lasso community.
©LassoSoft Inc 2015 | Web Development by Treefrog Inc | Privacy | Legal terms and Shipping | Contact LassoSoft