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

Google Analytics API - RemarketingAudiences.insert only working when linkedAdAccounts is AD_WORDS


I'm writing a Google Apps Script for creating audiences in Google Analytics. I keep getting the very unhelpful error message of There was an internal error.

As per [this guide][1], I am able to insert new audiences with the type AD_WORDS without issue. However my current task involves duplicating audiences of type ANALYTICS.

It seems that the linkedAdAccounts attribute of the resource I submit is incorrect. I can see that the official docs mention 3 possible options for the type: ADWORDS_LINKS, DBM_LINKS, MCC_LINKS or OPTIMIZE. Unfortunately, no detailed explanation is given for how these work other than ADWORDS_LINKS.

Here is the payload which is being rejected:

{
    name: "newName",
    linkedViews: ["123445677"],
    linkedAdAccounts: [
      {
        kind: "analytics#linkedForeignAccount",
        internalWebPropertyId: "12345678",
        status: "OPEN",
        remarketingAudienceId: "aaaaaaaaaaaaaaaaaaaaa",
        id: "xxxxxxxxxxxxxxxxxxxxx",
        webPropertyId: "UA-1234567-1",
        type: "ANALYTICS",
        accountId: "12345678",
      },
    ],
    audienceType: "SIMPLE",
    audienceDefinition: {
      includeConditions: {
        daysToLookBack: 7,
        segment: "users::condition::ga:sessionDuration>60",
        membershipDurationDays: 30,
        isSmartList: false,
      },
    },
  }


Solution

  • It turns out you can't add an id for an ANALYTICS linkedAdAccount. Just adding the following is sufficient.

    linkedAdAccounts: [
          {
            type: "ANALYTICS",
          },
        ],