Lasso Soft Inc. > Home

[google_analyticsdataexport]

Linkgoogle_analyticsdataexport
AuthorJason Huck
CategoryUtility
Version8.5.x
LicensePublic Domain
Posted07 Sep 2010
Updated07 Sep 2010
More by this author...

Description

Wrapper for the Google Analytics Data Export API. Requires [google_clientlogin][dictionary] and [xml_tree].

Sample Usage

// get a token via Client Login
var('token' = google_clientlogin(
	-Email='foo@foobar.com',
	-Passwd='sooperseekrit',
	-service='analytics'
));	

// create an object to interface with Google Analytics
var('gaData' = google_analyticsDataExport($token));

// get information about your Analytics account	
$gaData->getAccountData( -v=2, -prettyprint=true);	

// get the ID of a specific site
var('id') = $gaData->getTableIDByTitle('www.foobar.com');

// visits by browser & version for the past 30 days
var('reportData') = $gaData->getReportData(
	-ids=$id,
	-dimensions='ga:browser,ga:browserVersion',
	-metrics='ga:visits',
	-sort='-ga:visits',
	-startdate=date->subtract( -day=30)&,
	-enddate=date,
	-prettyprint=true
);

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

define_type(
	'analyticsdataexport',
	-namespace='google_',
	-prototype,
	-description='Wrapper for the Google Analytics Data Export API.'
);
	local(
		'tokens' = null,
		'accounts' = null
	);
	
	define_tag('oncreate', -req='tokens', -type='dictionary');
		self->tokens = #tokens;
	/define_tag;

	define_tag(
		'connect',
		-req='endpoint',
		-opt='args', -type='array',
		-opt='method',
		-encodenone
	);
		local('headers') = array(
			'Authorization' = 'GoogleLogin Auth=' + self->tokens->Auth,
			'GData-Version' = 2
		);
		
		local_defined('args') && !local_defined('method') ? local('method') = 'POST';
		
		protect;
			handle_error;
				local('response') = (local_defined('response') && #response->size ? #response | error_msg);
			/handle_error;
			
			if(local_defined('args'));
				if(#method == 'POST');
					local('response') = string(include_url(
						#endpoint,
						-postparams=#args,
						-sendmimeheaders=#headers,
						-timeout=30,
						-connecttimeout=30
					))->trim&;								
				else;
					local('response') = string(include_url(
						#endpoint,
						-getparams=#args,
						-sendmimeheaders=#headers,
						-timeout=30,
						-connecttimeout=30
					))->trim&;								
				/if;
			else;				
				local('response') = string(include_url(
					#endpoint,
					-sendmimeheaders=#headers,
					-timeout=30,
					-connecttimeout=30
				))->trim&;			
			/if;
		/protect;
		
		return(#response);		
	/define_tag;
	
	define_tag(
		'getAccountData',
		-opt='startindex', -type='integer',
		-opt='maxresults', -type='integer',
		-opt='v', -type='integer',
		-opt='prettyprint', -type='boolean'
	);
		local('args') = array;
		local_defined('startindex') ? #args->insert('start-index' = #startindex);
		local_defined('maxresults') ? #args->insert('max-results' = #maxresults);
		local_defined('v') ? #args->insert('v' = #v);
		local_defined('prettyprint') ? #args->insert('prettyprint' = #prettyprint);
	
		self->accounts = self->connect('https://www.google.com/analytics/feeds/accounts/default', #args, 'GET');
	/define_tag;
	
	define_tag(
		'getTableIDByTitle', 
		-req='title'
	);
		local(
			'id' = null,
			'entries' = xml_tree(self->accounts)->entry
		);
		
		iterate(#entries, local('i'));
			if(#i->title->contents == #title);
				local('id' = #i->tableId->contents);
				loop_abort;
			/if;
		/iterate;
		
		return(#id);
	/define_tag;
	
	define_tag(
		'getReportData',
		-req='ids',
		-opt='dimensions',
		-req='metrics',
		-opt='sort',
		-opt='filters',
		-opt='segment',
		-req='startdate', -type='date',
		-req='enddate', -type='date',
		-opt='startindex', -type='integer',
		-opt='maxresults', -type='integer',
		-opt='v', -type='integer',
		-opt='prettyprint', -type='boolean'
	);
		local('args') = array(
			'ids' = #ids,
			'metrics' = #metrics,
			'start-date' = #startdate->format('%Y-%m-%d'),
			'end-date' = #enddate->format('%Y-%m-%d')
		);
		
		local_defined('dimensions') ? #args->insert('dimensions' = #dimensions);
		local_defined('sort') ? #args->insert('sort' = #sort);
		local_defined('filters') ? #args->insert('filters' = #filters);
		local_defined('segment') ? #args->insert('segment' = #segment);
		local_defined('startindex') ? #args->insert('start-index' = #startindex);
		local_defined('maxresults') ? #args->insert('max-results' = #maxresults);
		local_defined('v') ? #args->insert('v' = #v);
		local_defined('prettyprint') ? #args->insert('prettyprint' = #prettyprint);
		
		return(self->connect('https://www.google.com/analytics/feeds/data', #args, 'GET'));
	/define_tag;
/define_type;

Comments

No comments

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft