Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet

15

Click here to load reader

description

Another in the 'do something useful with Google Apps Script' series. This time you'll see how to use the Analytics service and use exponential backoff to mitigate for quota rate limiting.

Transcript of Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet

Page 1: 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

Page 3: Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet

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

Page 4: Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet

Add analytics service

in advanced services, enable analytics

Page 5: Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet

Enable analytics on cloud console

Page 6: Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet

layout what you are going to dofunction myFunction() {

// open spreadsheet as database

// get all know analytics properties

// write to the sheet

}

Page 7: Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet

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;

});

}

Page 8: Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet

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

Page 9: Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet

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

Page 10: Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet

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

Page 11: Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet

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

Page 12: Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet

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);

}

Page 13: Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet

take a look at the sheetYou’ll find a row for each accounts profile you own

Page 14: Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet

Further homework

● Now you know how to find your internal analytics IDs, try using the Analytics service to do some queries