Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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

4,886 views

Published on

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.

Published in: Technology
  • Be the first to comment

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

  1. 1. do something useful with Apps Script in 5 minutes 4. Analytics property data to a sheet Bruce McPherson www.mcpher.com
  2. 2. 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
  3. 3. Add libraries to script ● create a spreadsheet ● get its id ● create a script ● Open resources ● Add references to libraries Mrckbr9_w7PCphJtOzhzA_Cz3TLx7pV4j MHfCjPQlweartW45xYs6hFai_d-phDA33 Mcbr-v4SsYKJP7JMohttAZyz3TLx7pV4j
  4. 4. Add analytics service in advanced services, enable analytics
  5. 5. Enable analytics on cloud console
  6. 6. layout what you are going to do function myFunction() { // open spreadsheet as database // get all know analytics properties // write to the sheet }
  7. 7. 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; }); }
  8. 8. extend function get all accounts second step - Loop through all your accounts to get the account records and use the account id to find the 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) { p.push(account); return p; },[]); }); }
  9. 9. extend function get webproperties third step - Loop through all the webproperties for each account id 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; },[]); }); }
  10. 10. extend function get profiles finally - Loop through all the profiles for each webproperties.id 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; },[]); }); }
  11. 11. write everything to a sheet Array data will be flattened as required to fit in 2d 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);
  12. 12. 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); }
  13. 13. take a look at the sheet You’ll find a row for each accounts profile you own
  14. 14. Further homework ● Now you know how to find your internal analytics IDs, try using the Analytics service to do some queries
  15. 15. Follow up materials Take a copy of this script Take a copy of these slides Join me on G+, or the G+ community More on desktop liberation More on database abstraction More on Analytics instrumentation More 5 minute things

×