GA4 | Google Analytics Admin API | Apps Script - google-apps-script

I am trying to call the (new) alpha GA Admin API for the simple task of listing all the Accounts that I have access to... I am at a stage where I call the API but I do not get any error message nor I see the information on the google sheet. Can you please help?
function listGA4Accounts() {
var sheet = _setupListGA4AccountsSheet();
var accounts = AnalyticsAdmin.Accounts.list();
if (accounts.items && accounts.items.length) {
for (var i = 0; i < accounts.items.length; i++) {
var account = accounts.items[i];
var rowNum = i+2;
sheet.getRange("A" + rowNum).setNumberFormat('#')
.setValue(account.name).setBackground(AUTO_POP_CELL_COLOR);
sheet.getRange("B" + rowNum)
.setValue(account.displayName).setBackground(AUTO_POP_CELL_COLOR);
sheet.getRange("C" + rowNum)
.setValue(account.createTime).setBackground(AUTO_POP_CELL_COLOR);
}
}
}
The above was adapted from the old code being used for Universal Analytics/GA3 and used to work just fine. What I am missing? I also have a standard GCP project in place and the API is enabled for that GCP project.
Any help/thoughts on the above are highly appreciated.
Thanks.

You have been quite close to the solution.
TIPS for debugging the API response:
prompt the API response in the console with Logger.log(JSON.stringify(<API RESPONSE>))
copy the log and paste it on a JSON formatter website like this one: https://jsonformatter.curiousconcept.com/
check the actual structure
OPTIONAL: copy the formatted JSON from the page & save it in the script as a variable and use this instead of the API response to prepare the code. Once it's working properly with the saved data you can switch back to the data from the API request.
Following things that I changed:
removed var sheet = _setupListGA4AccountsSheet(); (was not relevant for testing the API response)
just changed the way the JSON object is accessed bc it's a nested one, to get an account item it's necessary to write <variable name>.accounts.item
Here is the code that can be copied as-is to App Script editor and can be tested:
function listGA4Accounts() {
var accounts = AnalyticsAdmin.Accounts.list();
if (accounts && !accounts.error) {
accounts = accounts.accounts; // <== this is why it didn't work is a nested JSON
Logger.log(accounts[0]);
for (var i = 0, account; account = accounts[i]; i++) {
Logger.log(account);
/**
* PLACE your code here
*/
}
}
}

Related

Google App Script - How to monitor for new users

I wondered if anyone could point me in the right direction here?
I want to monitor the Google Workspace estate, and when a new user has been created send them an email. I’ve looked through the APIs but nothing is jumping out at me. But I know there are 3rd party tools out there that do this, so there’s got to be something I have missed?
I just created this script in Google Apps Script which gets and prints the list of all the users that were created today.
You can use this as a guide and keep testing with it. To accomplish this I used the Reports API to get the admin logs and get the list of all the users that were created today.
function myFunction() {
var userKey = 'all';
var applicationName = 'admin';
var optionalArgs = {
eventName:'CREATE_USER',
startTime: "2022-03-23T12:00:00.000Z",
fields : "items.events.parameters.value"
};
var rep = AdminReports.Activities.list(userKey,applicationName,optionalArgs);
const A = (JSON.parse(rep));
var totalUsers = Object.keys(A.items).length;
for(var i=0; i<totalUsers; i++)
{
var userEmail = A.items[i].events[0].parameters[0].value;
Logger.log(userEmail);
}
}
You would just need to change the startTime value according to the date you need to use and implement the part of sending the email now that you have all the email addresses.
References
API method: activities.list
Apps Script reference: Reports API

Getting a list of functions within your GAS project

I want to see if there is a way to obtain a list of all the functions I have in a Google Apps Script project. I've seen multiple threads on getting a list of all of your Google Apps Script projects but none as of yet for listing all of the functions in each project. Does anyone know if this is possible? I've looked through the Google Apps Script Reference Overview but I wasn't able to find anything that stood out to me (I, of course, could've missed it). If anyone has any suggestions, please let me know!.
The best example I can provide is:
I have a Google Spreadsheet file. Attached to that Google Spreadsheet is a GAS project (accessed through the Google Sheet menu "Tools -> Script Editor") that has a couple of different functions used to grab values from the sheet, do some calculations and post the results to a different sheet.
What I am trying to accomplish: Run some sort of function that can provide me a list of all of the functions I have in the GAS project (preferably as string values). Example would be:
["runMyCalculations","myOnEdit","sortClosedFiles","formatSheets"]
All of these are functions that can only be run if I open up the Script Editor and select it in the drop-down menu and click the "Run" button.
What I want to be able to do is create a dynamic list of all the functions I have so I can pass them into an "on open" triggered function that creates a custom menu in the sheet, listing out all of the functions I have. I want this so I can simply make changes to my sheet, go to the drop-down menu and run the function I need to run, rather than having to open up the Script Editor.
You can use the Apps Script API to get all the content out of an Apps Script file.
The following code has the option of passing in a file name to get. You must supply the Apps Script file ID. Passing in a gs file name is optional. Provided are 3 functions. The function that does all the work, a function to call that function with the parameters for testing, and a logging function. An OAuth library is not needed because the token is acquired from the ScriptApp service.
NOTE: You will need to enable the Apps Script API, and approve permission to your Drive in order for this code to work. Make sure to check the return from the UrlFetchApp.fetch() call the first time that you run this code for an error message. It may have a link that you need to use to enable the Apps Script API.
function getFuncNames(po) {
var allFiles,dataContentAsString,downloadUrl,fileContents,fileData,i,options,
theAccessTkn,thisFileName;
var ndxOfFunction=0,counter=0, ndxOfEnd=0, functionName="", allFncNames=[],
hasSpaces = 0;
var innerObj, thisFile, fileType = "", thisGS_Content,howManyFiles, allGsContent="";
/*
Get all script function names. If no gs file name is provided, the code
gets all the function names.
*/
/*
po.fileID - required - The Apps Script file ID
po.gsFileName - optional - the gs code file name to get - gets just one
file instead of all files
*/
//ll('po',po);
if (!po.fileID) {
return false;
}
theAccessTkn = ScriptApp.getOAuthToken();//Get an access token for OAuth
downloadUrl = "https://script.google.com/feeds/download/export?id=" +
po.fileID + "&format=json";//create url
options = {
"kind": "drive#file",
"id": po.fileID,
"downloadUrl": downloadUrl,
"headers": {
'Authorization': 'Bearer ' + theAccessTkn,
},
"contentType": "application/vnd.google-apps.script+json",
"method" : "GET"
};
fileData = UrlFetchApp.fetch(downloadUrl, options);//Get all the content from the Apps Script file
//ll('fileData',fileData)
dataContentAsString = fileData.getContentText();
fileContents = JSON.parse(dataContentAsString);//Parse string into object
allFiles = fileContents.files;//All the files in the Apps Script project
howManyFiles = allFiles.length;
for (i=0;i<howManyFiles;i++) {
thisFile = allFiles[i];//Get one inner element that represents one file
if (!thisFile) {continue;}
fileType = thisFile.type;
if (fileType !== "server_js") {continue;}//This is not a gs file - its HTML or json
thisFileName = thisFile.name;
//ll('typeof thisFileName',typeof thisFileName)
//ll('thisFileName',thisFileName)
//ll('equal',po.gsFileName !== thisFile.name)
if (po.gsFileName) {//Is there a setting for the file name to restrict the search to
if (po.gsFileName !== thisFile.name) {//The name to search for is not this file name
continue;
}
}
thisGS_Content = thisFile.source;//source is the key name for the file content
allGsContent = allGsContent + thisGS_Content;
}
//ll('allGsContent',allGsContent)
while (ndxOfFunction !== -1 || counter < 1000) {
ndxOfFunction = allGsContent.indexOf("function ");
//ll('ndxOfFunction',ndxOfFunction)
if (ndxOfFunction === -1) {break};
allGsContent = allGsContent.slice(ndxOfFunction+9);//Remove everything in front of 'function' first
ndxOfEnd = allGsContent.indexOf("(");
functionName = allGsContent.slice(0,ndxOfEnd);
allGsContent = allGsContent.slice(ndxOfEnd+2);//Remove the
hasSpaces = functionName.indexOf(" ");
if (hasSpaces !== -1) {continue;}
if (functionName.length < 150) {
allFncNames.push(functionName);
}//Any string over 150 long is probably not a function name
counter ++;
};
//ll('allFncNames',allFncNames)
return allFncNames;
};
function runOtherFnk() {
getFuncNames({fileID:"Your File ID here",gsFileName:"Code"});
}
function ll(a,b) {
//Logger.log(typeof a)
if (typeof b === 'object') {
b = JSON.stringify(b);
}
Logger.log(a + ":" + b)
}
The following code extracts file names from the this object:
function getAllFnks() {
var allFnks,fnkStr,k;
allFnks = [];
for (k in this) {
//Logger.log(k)
//Logger.log(typeof k)
//Logger.log(this[k])
//Logger.log(typeof this[k])
fnkStr = this[k];
if (fnkStr) {
fnkStr = fnkStr.toString();
//Logger.log(typeof fnkStr)
} else {
continue;
}
//Logger.log(fnkStr.toString().indexOf('function'))
if (fnkStr.indexOf('function') === 1) {
allFnks.push(k);
}
}
Logger.log(allFnks)
Logger.log('Number of functions: ' + allFnks.length)
}

Google Sheets Advanced Google Services URL Shortener 403 Error: Forbidden

I am trying to create a small application in in Google Sheets to sorten URLs on my personal google account. I am using the following code which I found here: Google Sheets Function to get a shortened URL (from Bit.ly or goo.gl etc.)
function onOpen() {
SpreadsheetApp.getUi()
.createMenu("Shorten")
.addItem("Go !!","rangeShort")
.addToUi()
}
function rangeShort() {
var range = SpreadsheetApp.getActiveRange(), data = range.getValues();
var output = [];
for(var i = 0, iLen = data.length; i < iLen; i++) {
//var url = UrlShortener.Url.insert({longUrl: data[i][0]});
var url = UrlShortener.Url.insert({longUrl: 'www.google.com'});
output.push([url.id]);
}
range.offset(0,1).setValues(output);
}
I created a new Google Cloud Project and enabled the URL shortener API in the project and on the Google sheet. The problem is that when I try and run the code I get an err on the line: var url = UrlShortener.Url.insert({longUrl: 'www.google.com'});
error 403, message:forbidden
when i try an execute the rangeShort() function. I have no idea how to fix this. Any ideas would be most appreciated! Thanks!
As it turns out, like Ruben mentioned, Google has moved away from their URL shortener. So after much research ans testing here is the solution:
Step 1
Migrate Google Cloud Project over to Firebase or create a new Firebase Project. See steps here
Step 2
Create a dummy project in order to create a base URL for the shortening. See this youtube video
Step 3
Get the Web API Key from your new Firebase Project (not the app you just created)
Step 4
Check the left side menu on the screen and navigate to Grow->Dynamic Links. You should see the new application you created and a URL at the top of the application. This will become the base of the new shortened URLs.
Step 5
Create the code in Google Apps Script inside the code builder from within Google Sheets. Here is the code that worked for me (I passed the url into this function) (This code is based on the answer found here):
function api_call(url){
var req='https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=[YOUR PROJECT WEB API KEY FROM STEP 3]';
var formData = {
"longDynamicLink": "[YOUR APPLICATION URL BASE FROM STEP 4]?link=" + url,
"suffix" : {
"option" : "UNGUESSABLE"
}
};
var options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(formData)
};
var response = UrlFetchApp.fetch(req, options);
var res=JSON.parse(response);
return res.shortLink;
}
Additional Information
Documentation on Creating Dynamic Links in Firebase
Documentation on using UrlFetchApp() in Google Apps Script
If the url shortener service was used in your project before March 30,2018
Instead of
www.google.com
use
https://www.google.com
Reference: https://developers.google.com/url-shortener/v1/url/insert
but if your project was created on or after March 30, 2018
From https://developers.google.com/url-shortener/v1/
Starting March 30, 2018, we will be turning down support for goo.gl URL shortener. Please see this blog post for detailed timelines and alternatives.
Just to be clear, please note, from the linked blog post:
For developers
Starting May 30, 2018, only projects that have accessed URL Shortener
APIs before today can create short links.
I can attest to #alutz's answer here with a small addition/correction to their code.
Use encodeURIcomponent() for the input url while assigning it to the Long Dynamic Link in case you have more than one custom parameters.
"longDynamicLink": "[YOUR APPLICATION URL BASE FROM STEP 4]?link=" + encodeURIcomponent(url),
This allowed me to pass in multiple arguments for my telegram bot like chat_id, text and parse_mode.

Google app scripts: email a spreadsheet as excel

How do you make an app script which attaches a spreadsheet as an excel file and emails it to a certain email address?
There are some older posts on Stackoverflow on how to do this however they seem to be outdated now and do not seem to work.
Thank you.
It looks like #Christiaan Westerbeek's answer is spot on but its been a year now since his post and I think there needs to be a bit of a modification in the script he has given above.
var url = file.exportLinks[MimeType.MICROSOFT_EXCEL];
There is something wrong with this line of code, maybe that exportLinks has now depreciated. When I executed his code it gave an error to the following effect:
TypeError: Cannot read property "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" from undefined.
The workaround is as follows:
The URL in the above line of code is basically the "download as xlsx" URL that can be used to directly download the spreadsheet as an xlsx file that you get from File> Download as > Microsoft Excel (.xlsx)
This is the format:
https://docs.google.com/spreadsheets/d/<<<ID>>>/export?format=xlsx&id=<<<ID>>>
where <<>> should be replaced by the ID of your file.
Check here to easily understand how to extract the ID from the URL of your google sheet.
Here's an up-to-date and working version. One prerequisite for this Google Apps script to work is that the Drive API v2 Advanced Google Service must be enabled. Enable it in your Google Apps script via Resources -> Advanced Google Services... -> Drive API v2 -> on. Then, that window will tell you that you must also enabled this service in the Google Developers Console. Follow the link and enable the service there too! When you're done, just use this script.
/**
* Thanks to a few answers that helped me build this script
* Explaining the Advanced Drive Service must be enabled: http://stackoverflow.com/a/27281729/1385429
* Explaining how to convert to a blob: http://ctrlq.org/code/20009-convert-google-documents
* Explaining how to convert to zip and to send the email: http://ctrlq.org/code/19869-email-google-spreadsheets-pdf
* New way to set the url to download from by #tera
*/
function emailAsExcel(config) {
if (!config || !config.to || !config.subject || !config.body) {
throw new Error('Configure "to", "subject" and "body" in an object as the first parameter');
}
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheetId = spreadsheet.getId()
var file = Drive.Files.get(spreadsheetId);
var url = 'https://docs.google.com/spreadsheets/d/'+spreadsheetId+'/export?format=xlsx';
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
var fileName = (config.fileName || spreadsheet.getName()) + '.xlsx';
var blobs = [response.getBlob().setName(fileName)];
if (config.zip) {
blobs = [Utilities.zip(blobs).setName(fileName + '.zip')];
}
GmailApp.sendEmail(
config.to,
config.subject,
config.body,
{
attachments: blobs
}
);
}
Update: I updated the way to set the url to download from. Doing it through the file.exportLinks collection is not working anymore. Thanks to #tera for pointing that out in his answer.

Use Google Script's Web App as Webhook to receive Push Notification directly

My Goal: Changes in Google Drive => Push Notification to https://script.google.com/a/macros/my-domain/... => App is pushed to take action.
I don't want to setup an middle Webhook agent for receiving notification. Instead, let the Web App (by Google Script) to receive it and be pushed directly.
Since the relevant function is quite undocumented (just here: https://developers.google.com/drive/web/push) , below is the code I tried but failure.
1. Is above idea feasible??
2. My code doPost(R) seems cannot receive notification (R parameter) properly. Anyway, no response after I change the Google Drive. Any problem? (I have tried to log the input parameter R so as to see its real structure and decide if the parameter Obj for OAuth is the same as normal Drive App, but error occur before log)
function SetWatchByOnce(){
var Channel = {
'address': 'https://script.google.com/a/macros/my-domain/.../exec',
'type': 'web_hook',
'id': 'my-UUID'
};
var Result = Drive.Changes.watch(Channel);
...
}
function doPost(R) {
var SysEmail = "My Email";
MailApp.sendEmail(SysEmail, 'Testing ', 'Successfully to received Push Notification');
var Response = JSON.parse(R.parameters);
if (Response.kind == "drive#add") {
var FileId = Response.fileId;
MyFile = DriveApp.getFolderById(FileId);
...
}
}
function doGet(e) {
var HTMLToOutput;
var SysEmail = "My Email";
if (e.parameters.kind) {
//I think this part is not needed, since Push Notification by Drive is via Post, not Get. I should use onPost() to receive it. Right?
} else if (e.parameters.code) {
getAndStoreAccessToken(e.parameters.code);
HTMLToOutput = '<html><h1>App is successfully installed.</h1></html>';
} else { //we are starting from scratch or resetting
HTMLToOutput = "<html><h1>Install this App now...!</h1><a href='" + getURLForAuthorization() + "'>click here to start</a></html>";
}
return HtmlService.createHtmlOutput(HTMLToOutput);
}
....
Cloud Functions HTTP trigger(s) might also be an option ...
(which not yet existed at time of this question). this just requires setting the trigger URL as the notification URL, in the Google Drive settings - and adding some NodeJS code for the trigger; whatever it shall do. one can eg. send emails and/or FCM push notifications alike that. that trigger could also be triggered from App Script, with UrlFetchApp and there is the App Script API. one can have several triggers, which are performing different tasks (App Script is only one possibilty).
Cicada,
We have done similar functions to receive webhooks/API calls many times. Notes:
to get R, you need: var Response = R.parameters and then you can do Response.kind, Response.id, etc.
Logger will not work with doGet() and doPost(). I set it up a write to spreadsheet -- before any serious code. That way I know if it is getting triggered.