[google_analyticsdataexport]

Description

Link: [google_analyticsdataexport]
Author: Jason Huck
Category: Utility
Version: 8.5.x
License: Public Domain
Posted: Sep. 07, 2010
Updated: Jan. 01, 0001
More by this author...

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

Parameters

none


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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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

none

Email:


Password:



Newest

Most Popular

Support tagSwap.net