Google Ads API is returning to beta status. Please read our blog post for more details.

Example: Creating a dynamic remarketing campaign with Google Merchant Center

In this example, you have a retail site that uploads product listings to Google Merchant Center and you want to remarket groups of products to users who visited the retail site. These users might have visited a product page or placed an item in a basket or checkout, recorded by the triggering of a remarketing tag. You can then create a campaign to target only this group or expand to similar audiences to increase your reach.

Read more about audience list generation best practices.

You will need the following to set up such a campaign:

  1. Remarketing List.
  2. Merchant Center account.
  3. Product Feed from Merchant Center.
  4. Media required for Responsive Ads for Display.

Here is the list of ad creatives that support Merchant Center product feeds:

Step 1 - Create a campaign linked to a Merchant Center product feed

In order to be able to remarket retail products, you'll need to create a campaign targeting the Display Network. This will require the following:

  • A CampaignBudget to specify how much the campaign is able to spend.
  • A UserList including the list of users to target with the campaign.
  • A ShoppingSetting to specify which Merchant Center account the product feed will be used for the campaign.

The basic steps for configuring the campaign are:

  1. Create a new campaign, setting the AdvertisingChannelType to DISPLAY.

  2. Set the CampaignBudget for the campaign. This can be a newly created budget or a shared budget.

  3. Set the ShoppingSetting for the campaign. Note that the CampaignService is used here, rather than AdGroupService, since the feed must be attached at campaign level.

    • First, set the merchantId to that of the Merchant Center account associated with the Google Ads account. This Merchant Center account must already be linked to Google Ads as described in this article.
    • Then set the campaignPriority.
    • Setting the enableLocal to true will include local inventory ads.
  4. Apply the mutate operation to ADD the new campaign to the account.

Code Example

private String createCampaign(
    GoogleAdsClient googleAdsClient,
    long customerId,
    long merchantCenterAccountId,
    long campaignBudgetId) {
  String budgetResourceName = ResourceNames.campaignBudget(customerId, campaignBudgetId);

  // Creates the campaign.
  Campaign campaign =
      Campaign.newBuilder()
          .setName(StringValue.of("Shopping campaign #" + System.currentTimeMillis()))
          // Dynamic remarketing campaigns are only available on the Google Display Network.
          .setAdvertisingChannelType(AdvertisingChannelType.DISPLAY)
          .setStatus(CampaignStatus.PAUSED)
          .setCampaignBudget(StringValue.of(budgetResourceName))
          .setManualCpc(ManualCpc.newBuilder().build())
          // The settings for the shopping campaign.
          // This connects the campaign to the merchant center account.
          .setShoppingSetting(
              ShoppingSetting.newBuilder()
                  .setCampaignPriority(Int32Value.of(0))
                  .setMerchantId(Int64Value.of(merchantCenterAccountId))
                  // Display Network campaigns do not support partition by country. The only
                  // supported value is "ZZ". This signals that products from all countries are
                  // available in the campaign. The actual products which serve are based on
                  // the products tagged in the user list entry.
                  .setSalesCountry(StringValue.of("ZZ"))
                  .setEnableLocal(BoolValue.of(true))
                  .build())
          .build();

  // Creates the campaign operation.
  CampaignOperation operation = CampaignOperation.newBuilder().setCreate(campaign).build();

  // Creates the campaign service client.
  try (CampaignServiceClient campaignServiceClient =
      googleAdsClient.getLatestVersion().createCampaignServiceClient()) {
    // Adds the campaign.
    MutateCampaignsResponse response =
        campaignServiceClient.mutateCampaigns(
            Long.toString(customerId), ImmutableList.of(operation));
    String campaignResourceName = response.getResults(0).getResourceName();
    System.out.printf("Created campaign with resource name '%s'.%n", campaignResourceName);
    return campaignResourceName;
  }
}

Step 2 - Create an ad group for the remarketing campaign

Code Example

private String createAdGroup(
    GoogleAdsClient googleAdsClient, long customerId, String campaignResourceName) {
  // Creates the ad group.
  AdGroup adGroup =
      AdGroup.newBuilder()
          .setName(StringValue.of("Dynamic remarketing ad group"))
          .setCampaign(StringValue.of(campaignResourceName))
          .setStatus(AdGroupStatus.ENABLED)
          .build();

  // Creates the ad group operation.
  AdGroupOperation operation = AdGroupOperation.newBuilder().setCreate(adGroup).build();

  // Creates the ad group service client.
  try (AdGroupServiceClient adGroupServiceClient =
      googleAdsClient.getLatestVersion().createAdGroupServiceClient()) {
    // Adds the ad group.
    MutateAdGroupsResponse response =
        adGroupServiceClient.mutateAdGroups(
            Long.toString(customerId), ImmutableList.of(operation));
    String adGroupResourceName = response.getResults(0).getResourceName();
    System.out.printf("Created ad group with resource name '%s'.%n", adGroupResourceName);
    return adGroupResourceName;
  }
}

Step 3 - Create a responsive ad for display

Code Example

private void createAd(
    GoogleAdsClient googleAdsClient, long customerId, String adGroupResourceName)
    throws IOException {
  String marketingImageUrl = "https://goo.gl/3b9Wfh";
  String marketingImageName = "Marketing Image";
  String marketingImageResourceName =
      uploadAsset(googleAdsClient, customerId, marketingImageUrl, marketingImageName);
  String logoImageName = "Logo Image";
  String logoImageUrl = "https://goo.gl/mtt54n";
  String logoImageResourceName =
      uploadAsset(googleAdsClient, customerId, logoImageUrl, logoImageName);

  // Creates the responsive display ad info object.
  ResponsiveDisplayAdInfo responsiveDisplayAdInfo =
      ResponsiveDisplayAdInfo.newBuilder()
          .addMarketingImages(
              AdImageAsset.newBuilder()
                  .setAsset(StringValue.of(marketingImageResourceName))
                  .build())
          .addSquareMarketingImages(
              AdImageAsset.newBuilder().setAsset(StringValue.of(logoImageResourceName)).build())
          .addHeadlines(AdTextAsset.newBuilder().setText(StringValue.of("Travel")).build())
          .setLongHeadline(
              AdTextAsset.newBuilder().setText(StringValue.of("Travel the World")).build())
          .addDescriptions(
              AdTextAsset.newBuilder().setText(StringValue.of("Take to the air!")).build())
          .setBusinessName(StringValue.of("Interplanetary Cruises"))
          // Optional: Call to action text.
          // Valid texts: https://support.google.com/adwords/answer/7005917
          .setCallToActionText(StringValue.of("Apply Now"))
          // Optional: Creates a logo image and set it to the ad.
          .addLogoImages(
              AdImageAsset.newBuilder().setAsset(StringValue.of(logoImageResourceName)).build())
          // Optional: Creates a logo image and set it to the ad.
          .addSquareLogoImages(
              AdImageAsset.newBuilder().setAsset(StringValue.of(logoImageResourceName)).build())
          // Whitelisted accounts only: Sets color settings using hexadecimal values.
          // Sets allowFlexibleColor to false if you want your ads to render by always
          // using your colors strictly.
          /*
          .setMainColor(StringValue.of("#0000ff"))
          .setAccentColor(StringValue.of("#ffff00"))
          .setAllowFlexibleColor(BoolValue.of(false))
          */
          // Whitelisted accounts only: Sets the format setting that the ad will be
          // served in.
          /*
          .setFormatSetting(DisplayAdFormatSetting.NON_NATIVE)
          */
          .build();

  // Creates the ad.
  Ad ad =
      Ad.newBuilder()
          .setResponsiveDisplayAd(responsiveDisplayAdInfo)
          .addFinalUrls(StringValue.of("http://www.example.com/"))
          .build();

  // Creates the ad group ad.
  AdGroupAd adGroupAd =
      AdGroupAd.newBuilder().setAdGroup(StringValue.of(adGroupResourceName)).setAd(ad).build();

  // Creates the ad group ad operation.
  AdGroupAdOperation operation = AdGroupAdOperation.newBuilder().setCreate(adGroupAd).build();

  // Creates the ad group ad service client.
  try (AdGroupAdServiceClient adGroupAdServiceClient =
      googleAdsClient.getLatestVersion().createAdGroupAdServiceClient()) {
    // Adds the ad group ad.
    MutateAdGroupAdsResponse response =
        adGroupAdServiceClient.mutateAdGroupAds(
            Long.toString(customerId), ImmutableList.of(operation));
    System.out.printf(
        "Created ad group ad with resource name '%s'.%n",
        response.getResults(0).getResourceName());
  }
}

Step 4 - Target a user list

Code Example

private void attachUserList(
    GoogleAdsClient googleAdsClient,
    long customerId,
    String adGroupResourceName,
    long userListId) {
  String userListResourceName = ResourceNames.userList(customerId, userListId);
  // Creates the ad group criterion that targets the user list.
  AdGroupCriterion adGroupCriterion =
      AdGroupCriterion.newBuilder()
          .setAdGroup(StringValue.of(adGroupResourceName))
          .setUserList(
              UserListInfo.newBuilder().setUserList(StringValue.of(userListResourceName)).build())
          .build();

  // Creates the ad group criterion operation.
  AdGroupCriterionOperation operation =
      AdGroupCriterionOperation.newBuilder().setCreate(adGroupCriterion).build();

  // Creates the ad group criterion service client.
  try (AdGroupCriterionServiceClient adGroupCriterionServiceClient =
      googleAdsClient.getLatestVersion().createAdGroupCriterionServiceClient()) {
    // Adds the ad group criterion.
    MutateAdGroupCriteriaResponse response =
        adGroupCriterionServiceClient.mutateAdGroupCriteria(
            Long.toString(customerId), ImmutableList.of(operation));
    System.out.printf(
        "Created ad group criterion with resource name '%s'.%n",
        response.getResults(0).getResourceName());
  }
}

Once you've set up the dynamic remarketing campaign for your Merchant Center feeds, you can use the Ad Preview tool from your Google Ads account to see the contents pulled from your Merchant Center feeds.