首页 > 解决方案 > 使用 Google Script Apps 将受众插入 Google Analytics 时出现问题

问题描述

我有一个 Google 表格,其中包含我想在 Google Analytics 中创建的受众信息。我正在尝试使用 Google Apps 脚本插入这些受众,并采用 Google Sheet 的值。

当我运行我的函数时,我收到了这个错误:analytics.management.remarketingAudience.insert; 错误:无效的 accountId:UA-143962394-1。

如果我更改参数的顺序,那么插入函数调用如下所示:

Analytics.Management.RemarketingAudience.insert(propertyId,accountId,resource2)

我收到此错误:analytics.management.remarketingAudience.insert; 错误:解析错误

你知道我的代码有什么问题吗?

我尝试将资源参数作为 json、对象和字符串传递,但结果是一样的。

function createAudience(){
    var data = readSpreadsheetData()
    Logger.log(data)    
    var resource = {
                name: data.audiences[0].name,
                linkedViews: [getViewId(data.country)],
                linkedAdAccounts: [{
                        type: data.audiences[0].type,
                        linkedAccountId: data.audiences[0].linkedAccountId
                }],
                audienceType: data.audiences[0].audienceType,
                stateBasedAudienceDefinition: {
                    includeConditions: {
                        daysToLookBack: data.audiences[0].daysToLookBack,
                        segment: data.audiences[0].segment,
                        membershipDurationDays: data.audiences[0].membershipDurationDays,
                        isSmartList: data.audiences[0].isSmartList
                    },
                }
            }
    var accountId = data.accountId, propertyId = getPropertyId(data.country)
    Logger.log(resource)
    var request = Analytics.Management.RemarketingAudience.insert(accountId,propertyId,resource)
    request.execute(function (response) { Logger.log(response) });    
}

标签: google-apps-scriptgoogle-analytics-api

解决方案


  • 您想通过 Google Apps 脚本使用“再营销受众:插入”的方法。

如果我的理解是正确的,那么这个修改呢?我认为您的请求正文是正确的。那么这个改装怎么样呢?

修改后的脚本:

在使用此脚本之前,请确认是否在 Advanced Google services 中启用了 Google Analytics API

从:

var request = Analytics.Management.RemarketingAudience.insert(accountId,propertyId,resource)
request.execute(function (response) { Logger.log(response) });

至:

var response = Analytics.Management.RemarketingAudience.insert(resource,accountId,propertyId);
Logger.log(response);

笔记:

  • 当使用脚本编辑器的自动完成时,它是Analytics.Management.RemarketingAudience.insert(resource, accountId, webPropertyId). 这样resource, accountId, propertyId就可以确认顺序了。
  • 如果您的每个参数resource, accountId, propertyId都有问题,则请求会返回错误。届时,请确认参数。

参考:

如果这没有解决您的问题,我深表歉意。


推荐阅读