Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet
Click here to load reader
-
Upload
bruce-mcpherson -
Category
Technology
-
view
2.701 -
download
1
description
Transcript of Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet
do something useful with Apps Script in 5 minutes
4. Analytics property data to a sheetBruce McPhersonwww.mcpher.com
Snippet objectives● Use the lessons learned in ‘using a spreadsheet as a
database’● Gets the properties information for all your analytics
profiles● Shows how to avoid rate limit exceeded problems● Flattens and writes it all to a sheet
Libraries used
● database abstraction● driver sheet● useful stuff
Add libraries to script
● create a spreadsheet● get its id● create a script● Open resources● Add references to librariesMrckbr9_w7PCphJtOzhzA_Cz3TLx7pV4j
MHfCjPQlweartW45xYs6hFai_d-phDA33
Mcbr-v4SsYKJP7JMohttAZyz3TLx7pV4j
Add analytics service
in advanced services, enable analytics
Enable analytics on cloud console
layout what you are going to dofunction myFunction() {
// open spreadsheet as database
// get all know analytics properties
// write to the sheet
}
write function to get properties - 1
First step - the analytics service is rate limited, so we have to handle retries using exponential backoff. You’ll find that in the cUseful library. Create a wrapper for the code we’re about to add
function findAllProperties () {
// find all the properties belonging to you
return cUseful.rateLimitExpBackoff( function () {
var am = Analytics.Management;
});
}
extend function get all accounts
function findAllProperties () {
// find all the properties belonging to you
return cUseful.rateLimitExpBackoff( function () {
var am = Analytics.Management;
return (am.Accounts.list().items || []).reduce (function(p,account) {
p.push(account);
return p;
},[]);
});
}
second step - Loop through all your accounts to get the account records and use the account id to find the webproperties
extend function get webproperties
function findAllProperties () {
// find all the properties belonging to you
return cUseful.rateLimitExpBackoff( function () {
var am = Analytics.Management;
return (am.Accounts.list().items || []).reduce (function(p,account) {
(am.Webproperties.list(account.id).items || []).forEach(function(webProperty) {
p.push(webProperty);
});
return p;
},[]);
});
}
third step - Loop through all the webproperties for each account id
extend function get profiles
function findAllProperties () {
// find all the properties belonging to you
return cUseful.rateLimitExpBackoff( function () {
var am = Analytics.Management;
return (am.Accounts.list().items || []).reduce (function(p,account) {
(am.Webproperties.list(account.id).items || []).forEach(function(webProperty) {
(am.Profiles.list(account.id,webProperty.id).items || [] ).forEach( function (profile) {
p.push(profile);
});
});
return p;
},[]);
});
}
finally - Loop through all the profiles for each webproperties.id
write everything to a sheet
// write to the sheet after deleting current contents
var result = sheetHandle.remove();
if (result.handleCode < 0 ) throw JSON.stringify(result);
var result = sheetHandle.save(properties);
if (result.handleCode < 0 ) throw JSON.stringify(result);
Array data will be flattened as required to fit in 2d sheet
Here’s the whole thing function myFunction() {
// open spreadsheet as database
var sheetHandle = new cDbAbstraction.DbAbstraction (cDriverSheet, {
siloid:'analytics',
dbid:'19tZRW5CxA4V0kjJX8yAXAGGjzvVZ1433vz-NmBIBt7U'
});
if (!sheetHandle.isHappy()) throw 'unable to open sheet';
// get all know analytics properties
var properties = findAllProperties();
// write to the sheet after deleting current contents
var result = sheetHandle.remove();
if (result.handleCode < 0 ) throw JSON.stringify(result);
var result = sheetHandle.save(properties);
if (result.handleCode < 0 ) throw JSON.stringify(result);
}
take a look at the sheetYou’ll find a row for each accounts profile you own
Further homework
● Now you know how to find your internal analytics IDs, try using the Analytics service to do some queries
Follow up materials
Take a copy of this scriptTake a copy of these slidesJoin me on G+, or the G+ communityMore on desktop liberationMore on database abstractionMore on Analytics instrumentationMore 5 minute things