|
| 1 | +/** |
| 2 | + * Copyright 2024 Knowit Experience Oslo |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/* **** GOOGLE ANALYTICS 4 ADMIN API |
| 18 | +Get Account & Property Names & ID |
| 19 | +Developer documentation: |
| 20 | +https://developers.google.com/analytics/devguides/config/admin/v1 |
| 21 | +*/ |
| 22 | + |
| 23 | +// **** LIST GA4 ACCOUNTS |
| 24 | +function listGA4Accounts() { |
| 25 | + try { |
| 26 | + let pageToken; // Variable to hold the current page token |
| 27 | + const accountList = []; |
| 28 | + |
| 29 | + do { |
| 30 | + // Fetch the account summaries with the current page token |
| 31 | + const response = AnalyticsAdmin.AccountSummaries.list({ pageToken: pageToken }); |
| 32 | + |
| 33 | + // Process the account summaries |
| 34 | + if (response.accountSummaries && response.accountSummaries.length) { |
| 35 | + for (let i = 0; i < response.accountSummaries.length; i++) { |
| 36 | + const account = response.accountSummaries[i]; |
| 37 | + if (account.propertySummaries) { |
| 38 | + properties = AnalyticsAdmin.Properties.list({ filter: 'parent:' + account.account }); |
| 39 | + if (properties.properties !== null) { |
| 40 | + accountList.push([account.displayName + ' - ' + account.name.replace('accountSummaries/', '')]); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // Update the page token for the next iteration |
| 47 | + pageToken = response.nextPageToken; |
| 48 | + } while (pageToken); // Continue until there is no more page token |
| 49 | + |
| 50 | + // Write the account list to the sheet |
| 51 | + if(accountList && accountList.length) { |
| 52 | + const sheet = ss.getSheetByName(helperGA4Tab); |
| 53 | + const startRow = 2; |
| 54 | + const numRows = sheet.getLastRow(); // The number of row to clear |
| 55 | + sheet.getRange(startRow, 2, numRows + 1, sheet.getLastColumn()).clearContent(); |
| 56 | + ss.getSheetByName(settingsTab).getRange(8, 2, 2, 1).clearContent(); |
| 57 | + |
| 58 | + sheet.getRange(startRow, 1, accountList.length, accountList[0].length).setValues(accountList); |
| 59 | + } |
| 60 | + } catch (err) { |
| 61 | + Logger.log('listGA4Accounts: ' + err.stack); |
| 62 | + SpreadsheetApp.getUi().alert('Error occurred in "listGA4Accounts" function: \n' + err); |
| 63 | + } |
| 64 | +} |
| 65 | +// **** END LIST GA4 ACCOUNTS |
| 66 | + |
| 67 | +// **** LIST GA4 PROPERTIES |
| 68 | +function listGA4Properties() { |
| 69 | + try { |
| 70 | + if (!ga4AccountID) { |
| 71 | + SpreadsheetApp.getUi().alert("GA4 Account is not set in Settings Sheet."); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + const sheet = ss.getSheetByName(helperGA4Tab); |
| 76 | + const startRow = 2; |
| 77 | + const numRows = sheet.getLastRow(); // The number of row to clear |
| 78 | + sheet.getRange(startRow, 2, numRows + 1, sheet.getLastColumn()).clearContent(); |
| 79 | + |
| 80 | + if (ga4AccountID) { |
| 81 | + let pageToken; // Variable to hold the current page token |
| 82 | + const propertyList = []; |
| 83 | + |
| 84 | + do { |
| 85 | + // Fetch the properties with the current page token |
| 86 | + const response = AnalyticsAdmin.Properties.list({ filter: 'ancestor:accounts/' + ga4AccountID, pageToken: pageToken }); |
| 87 | + |
| 88 | + if (response.properties && response.properties.length) { |
| 89 | + for (let i = 0; i < response.properties.length; i++) { |
| 90 | + const property = response.properties[i]; |
| 91 | + if (property) { |
| 92 | + propertyList.push([property.displayName + ' - ' + property.name.replace('properties/', '')]); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // Update the page token for the next iteration |
| 98 | + pageToken = response.nextPageToken; |
| 99 | + } while (pageToken); // Continue until there is no more page token |
| 100 | + |
| 101 | + // Write the property list to the sheet |
| 102 | + if (propertyList && propertyList.length) { |
| 103 | + sheet.getRange(startRow, 2, propertyList.length, propertyList[0].length).setValues(propertyList); |
| 104 | + sheet.getRange(startRow, 1, numRows + 1, 1).clearContent(); |
| 105 | + |
| 106 | + const account = AnalyticsAdmin.Accounts.get({}, { name: `accounts/${ga4AccountID}` }); |
| 107 | + sheet.getRange(startRow, 1).setValue(account.displayName + ' - ' + account.name.replace('accounts/', '')); |
| 108 | + } |
| 109 | + } |
| 110 | + } catch (err) { |
| 111 | + Logger.log('listGA4Properties: ' + err.stack); |
| 112 | + SpreadsheetApp.getUi().alert('Error occurred in "listGA4Properties" function: \n' + err); |
| 113 | + } |
| 114 | +} |
| 115 | +// **** END LIST GA4 PROPERTIES |
0 commit comments