Search code examples
google-apps-scriptgoogle-analytics-api

Problem inserting audiences into Google Analytics using Google Script Apps


I have a Google Sheet with the information of the audiences that I want to create in Google Analytics. I'm trying to insert that audiences using Google Apps Script, taking the values of the Google Sheet.

When I run my function, I get this error: analytics.management.remarketingAudience.insert; error: invalid accountId: UA-143962394-1.

If I change the orders of the parameters, so the insert function call looks like this:

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

I get this error: analytics.management.remarketingAudience.insert; error: Parse Error

Do you know what is wrong with my code?

I tried to pass resource parameter as json, object and string but the result was the same.

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) });    
}

Solution

    • You want to use the method of "Remarketing Audiences: insert" using Google Apps Script.

    If my understanding is correct, how about this modification? I think that your request body is correct. So how about this modification?

    Modified script:

    Before you use this script, please confirm whether Google Analytics API is enabled at Advanced Google services.

    From:

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

    To:

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

    Note:

    • When the autocomplete of the script editor is used, it is Analytics.Management.RemarketingAudience.insert(resource, accountId, webPropertyId). So the order of resource, accountId, propertyId can be confirmed.
    • If each parameter of your resource, accountId, propertyId has the issues, the request returns an error. At that time, please confirm the parameters.

    References:

    If this didn't resolve your issue, I apologize.