Last active
June 7, 2016 04:13
-
-
Save MatthewDaniels/31554030ac674f04b677643d079e05bc to your computer and use it in GitHub Desktop.
A simple google apps script script for Google Sheets that replaces the current sheet's data with Property & View information for a specific Google Analytics Account. You will need to enable access via the API to your account - see: https://developers.google.com/apps-script/guides/services/advanced ... NOTE: THIS IS QUITE MESSY AND HAS NO CHEKCIN…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getSummary() { | |
var listed = Analytics.Management.AccountSummaries.list(); | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = ss.getActiveSheet(); | |
// var baseRangeRow = 4 | |
// sheet.getRange(baseRangeRow,1).setValue(listed.items[0]); | |
// sheet.getRange(baseRangeRow+1,1).setValue(listed.items[0].webProperties[0]); | |
// sheet.getRange(baseRangeRow+2,1).setValue(listed.items[0].webProperties[0].profiles[0]); | |
// sheet.getRange(2, 1, listed.items.length, listed.items[0].webProperties.length) | |
// Logger.log(listed.items); | |
var allProfiles = []; | |
// prepare the data for the sheet | |
// 1. iterate over the listed.items | |
// 2. iterate over the webProperties within that | |
for(var i = 0; i < listed.items.length; i++ ) { | |
var listedAccount = listed.items[i], | |
accountName = listedAccount.name; | |
for(var j = 0; j < listedAccount.webProperties.length; j++) { | |
var currentProperty = listedAccount.webProperties[j], | |
propertyName = currentProperty.name, | |
propertyId = currentProperty.id, | |
propertyLevel = currentProperty.level, | |
propertyDefaultUrl = currentProperty.websiteUrl, | |
propertyProfiles = currentProperty.profiles, | |
propertyProfileCount = propertyProfiles.length, | |
profiles = []; | |
if(j>0){ | |
// we only want the account name against the first item in the list | |
accountName = ''; | |
} | |
for(var k = 0; k < currentProperty.profiles.length; k++) { | |
var currentView = currentProperty.profiles[k], | |
viewName = currentView.name, | |
viewId = currentView.id, | |
viewType = currentView.type; | |
if(k>0){ | |
// we only want the account name against the first item in the list | |
accountName = ''; | |
// propertyName = ''; | |
// propertyId = ''; | |
// propertyLevel = ''; | |
propertyDefaultUrl = ''; | |
propertyProfileCount = ''; | |
} | |
profiles[k] = [ | |
accountName, | |
propertyName, | |
propertyId, | |
propertyLevel, | |
propertyDefaultUrl, | |
propertyProfileCount, | |
viewName, | |
viewId, | |
viewType | |
]; | |
allProfiles.push(profiles[k]); | |
} | |
} | |
} | |
// var header = [['Account Name','Property Name','Property UA Code','Property Level','Property Default URL','View Count','View Name','View ID','View Type1']]; | |
// sheet.getRange(1,1,header.length, 1).setValues(header[0]); | |
// populate the sheet | |
sheet.getRange(2, 1, allProfiles.length, allProfiles[0].length).setValues(allProfiles); | |
} | |
/************************************************************************** | |
* Main function runs on application open, setting the menu of commands | |
*/ | |
function onOpen(e) { | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var ui = SpreadsheetApp.getUi(); | |
// create the addon menu | |
try { | |
var menu = ui.createAddonMenu(); | |
if (e && e.authMode == ScriptApp.AuthMode.NONE) { | |
// Add a normal menu item (works in all authorization modes). | |
menu.addItem('List Account Summary', 'getSummary') | |
// .addSeparator() | |
// .addItem('List custom dimensions', 'requestDimensionList') | |
// .addItem('Update custom dimensions', 'requestDimensionUpdate') | |
} else { | |
menu.addItem('List Account Summary', 'getSummary') | |
// .addItem('Update filters', 'requestFilterUpdate') | |
// .addSeparator() | |
// .addItem('List custom dimensions', 'requestDimensionList') | |
// .addItem('Update custom dimensions', 'requestDimensionUpdate') | |
// .addSeparator() | |
// .addItem('List custom metrics', 'requestMetricList') | |
// .addItem('Update custom metrics', 'requestMetricUpdate') | |
} | |
menu.addToUi(); | |
} catch (e) { | |
Browser.msgBox(e.message); | |
} | |
} | |
/************************************************************************** | |
* Install function runs when the application is installed | |
*/ | |
function onInstall(e) { | |
onOpen(e); | |
} | |
/** | |
* Shows the side bar populated with the content from the instructions page | |
*/ | |
//function about() { | |
// var html = HtmlService.createHtmlOutputFromFile('about') | |
// .setSandboxMode(HtmlService.SandboxMode.IFRAME) | |
// .setTitle('About') | |
// .setWidth(300); | |
// | |
// SpreadsheetApp.getUi().showSidebar(html); | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment