使用 Google Analytics 管理 API 和应用脚本提取 GA4 媒体资源列表
想知道是否有人使用新的Analytics Admin API将GA帐户和属性列表导出到电子表格中。
过去,我已经将管理API用于此目的,但这将我们限制在UA属性上,我希望在这里还包括GA4属性。
我已经拍摄了将旧脚本转换为新API的镜头,但是我什至没有成功地提取帐户名称。
function listGA4Accounts() {
var createss = SpreadsheetApp.create("Google Analytics Accounts");
var ssid = createss.getId();
var openss = SpreadsheetApp.openById(ssid);
var insertsheet = openss.insertSheet('Google Analytics Schema');
insertsheet.setFrozenRows(1);
insertsheet.getRange("A1:D1").setValues([['displayName', 'Account ID', 'Property Name', 'Property ID']]);
var sheet = SpreadsheetApp.openById(createss.getId()).getSheetByName("Google Analytics Schema")
var accounts = AnalyticsAdmin.Accounts.list();
if (accounts && !accounts.error) {
accounts = accounts.accounts;
// Logger.log(accounts[0]);
for (var i = 0, account; account = accounts[i]; i++) {
sheet.appendRow([accounts.accounts[i].displayName]);
}
}
}
Wondering if anyone has experience exporting a list of GA accounts and properties to a spreadsheet using the new Analytics Admin API.
I've used the Management API for this purpose in the past but that limits us to UA properties and I want to hopefully include GA4 properties here as well.
I've taken a shot at converting an old script to the new API but I haven't even succeeded in pulling in account names.
function listGA4Accounts() {
var createss = SpreadsheetApp.create("Google Analytics Accounts");
var ssid = createss.getId();
var openss = SpreadsheetApp.openById(ssid);
var insertsheet = openss.insertSheet('Google Analytics Schema');
insertsheet.setFrozenRows(1);
insertsheet.getRange("A1:D1").setValues([['displayName', 'Account ID', 'Property Name', 'Property ID']]);
var sheet = SpreadsheetApp.openById(createss.getId()).getSheetByName("Google Analytics Schema")
var accounts = AnalyticsAdmin.Accounts.list();
if (accounts && !accounts.error) {
accounts = accounts.accounts;
// Logger.log(accounts[0]);
for (var i = 0, account; account = accounts[i]; i++) {
sheet.appendRow([accounts.accounts[i].displayName]);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也一直在为此苦苦挣扎。以下是我经过大量尝试和错误后得出的结论:
并不是说这是正确的方法...但它似乎确实能够提取我的所有 UA 和 GA4 属性。
如果您只需要 GA4 媒体资源,则可以省略“if (account.propertySummaries)”上的 else。
I've been struggling with this as well. Here's what I came up with after a lot of trail and error:
Not saying it's the right way...but it does appear to be able to pull all of my UA and GA4 properties.
If you only want GA4 properties then you can leave out the else on "if (account.propertySummaries)".