The AdWords API will sunset on April 27, 2022. Migrate to the Google Ads API to take advantage of the latest Google Ads features.

Shopping Campaign Samples

The code samples below provide examples for managing Shopping campaigns using the AdWords API. Client Library.

Build a product partition tree for an ad group

#!/usr/bin/perl -w
#
# Copyright 2017, Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This example creates a ProductPartition tree.

use strict;
use lib "../../../lib";
use utf8;

use Data::Uniqid qw(uniqid);

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201809::AdGroupCriterionOperation;
use Google::Ads::AdWords::v201809::BiddableAdGroupCriterion;
use Google::Ads::AdWords::v201809::BiddingStrategyConfiguration;
use Google::Ads::AdWords::v201809::CpcBid;
use Google::Ads::AdWords::v201809::Criterion;
use Google::Ads::AdWords::v201809::Money;
use Google::Ads::AdWords::v201809::NegativeAdGroupCriterion;
use Google::Ads::AdWords::v201809::ProductBiddingCategory;
use Google::Ads::AdWords::v201809::ProductCanonicalCondition;
use Google::Ads::AdWords::v201809::ProductBrand;
use Google::Ads::AdWords::v201809::ProductPartition;

use Cwd qw(abs_path);

# Replace with valid values of your account.
my $ad_group_id = "INSERT_AD_GROUP_ID_HERE";

# Example main subroutine.
sub add_product_partition_tree {
  my ($client, $ad_group_id) = @_;

  my $operations = [];

  # The next temporary criterion ID to be used.
  #
  # When creating our tree we need to specify the parent-child relationships
  # between nodes. However, until a criterion has been created on the server
  # we do not have a criterionId with which to refer to it.
  #
  # Instead we can specify temporary IDs that are specific to a single mutate
  # request. Once the criteria have been created they are assigned an ID as
  # normal and the temporary ID will no longer refer to it.
  #
  # A valid temporary ID is any negative integer.
  my $next_id = -1;

  # The most trivial partition tree has only a unit node as the root:
  #   create_unit($operations, $ad_group_id, null, null, 100000);
  my $root = create_subdivision($operations, $next_id--, $ad_group_id);

  create_unit(
    $operations,
    $ad_group_id,
    $root,
    Google::Ads::AdWords::v201809::ProductCanonicalCondition->new(
      {condition => "NEW"}
    ),
    200000
  );

  create_unit(
    $operations,
    $ad_group_id,
    $root,
    Google::Ads::AdWords::v201809::ProductCanonicalCondition->new(
      {condition => "USED"}
    ),
    100000
  );

  my $other_condition =
    create_subdivision($operations, $next_id--, $ad_group_id, $root,
    Google::Ads::AdWords::v201809::ProductCanonicalCondition->new());

  create_unit($operations, $ad_group_id, $other_condition,
    Google::Ads::AdWords::v201809::ProductBrand->new({value => "CoolBrand"}),
    900000);

  create_unit($operations, $ad_group_id, $other_condition,
    Google::Ads::AdWords::v201809::ProductBrand->new({value => "CheapBrand"}),
    10000);

  my $other_brand =
    create_subdivision($operations, $next_id--, $ad_group_id, $other_condition,
    Google::Ads::AdWords::v201809::ProductBrand->new());

  # The value for the bidding category is a fixed ID for the 'Luggage & Bags'
  # category. You can retrieve IDs for categories from the ConstantDataService.
  # See the 'get_product_category_taxonomy' example for more details.
  create_unit(
    $operations,
    $ad_group_id,
    $other_brand,
    Google::Ads::AdWords::v201809::ProductBiddingCategory->new({
        type  => "BIDDING_CATEGORY_L1",
        value => -5914235892932915235
      }
    ),
    750000
  );

  create_unit(
    $operations,
    $ad_group_id,
    $other_brand,
    Google::Ads::AdWords::v201809::ProductBiddingCategory->new(
      {type => "BIDDING_CATEGORY_L1",}
    ),
    110000
  );

  my $result =
    $client->AdGroupCriterionService()->mutate({operations => $operations});

  my $children = {};
  my $root_node;
  # For each criterion, make an array containing each of its children.
  # We always create the parent before the child, so we can rely on that here.
  foreach my $ad_group_criterion (@{$result->get_value()}) {
    $children->{$ad_group_criterion->get_criterion()->get_id()} = [];

    my $parent_criterion_id =
      $ad_group_criterion->get_criterion()->get_parentCriterionId();
    if ($parent_criterion_id) {
      push $children->{$parent_criterion_id},
        $ad_group_criterion->get_criterion();
    } else {
      $root_node = $ad_group_criterion->get_criterion();
    }
  }

  # Show the tree
  display_tree($root_node, $children);

  return 1;
}

# Return a new subdivision product partition and add to the provided list
# an operation to create the partition. The parent and value fields
# should not be specified for the root node.
# operations: The list of operations to add to.
# temp_id: The temporary ID to use for the new partition.
# ad_group_id: The ID of the ad group for the new partition.
# parent: (Optional) The parent partition for the new partition.
# value: (Optional) The case value (product dimension) for the new partition.
sub create_subdivision {
  my ($operations, $temp_id, $ad_group_id, $parent, $value) = @_;
  my $division = Google::Ads::AdWords::v201809::ProductPartition->new({
      partitionType => "SUBDIVISION",
      id            => $temp_id
  });

  # The root node has neither a parent nor a value.
  if ($parent) {
    $division->set_parentCriterionId($parent->get_id());
    $division->set_caseValue($value);
  }

  my $criterion = Google::Ads::AdWords::v201809::BiddableAdGroupCriterion->new({
      adGroupId => $ad_group_id,
      criterion => $division
  });

  push $operations, create_add_operation($criterion);

  return $division;
}

# Return a new unit product partition and add to the provided list
# an operation to create the partition. The parent, value and bid_amount
# fields should not be specified for the root node.
# operations: The list of operations to add to.
# ad_group_id: The ID of the ad group for the new partition.
# parent: (Optional) The parent partition for the new partition.
# value: (Optional) The case value (product dimension) for the new partition.
# bid_amount: (Optional) The bid amount for the AdGroupCriterion.  If specified
#   then the AdGroupCriterion will be a BiddableAdGroupCriterion.
sub create_unit {
  my ($operations, $ad_group_id, $parent, $value, $bid_amount) = @_;
  my $unit = Google::Ads::AdWords::v201809::ProductPartition->new(
    {partitionType => "UNIT",});

  # The root node has neither a parent nor a value.
  if ($parent) {
    $unit->set_parentCriterionId($parent->get_id());
    $unit->set_caseValue($value);
  }

  my $criterion;
  if ($bid_amount && $bid_amount > 0) {
    my $biddingStrategyConfiguration =
      Google::Ads::AdWords::v201809::BiddingStrategyConfiguration->new({
        bids => [
          Google::Ads::AdWords::v201809::CpcBid->new({
              bid => Google::Ads::AdWords::v201809::Money->new(
                {microAmount => $bid_amount})})]});

    $criterion =
      Google::Ads::AdWords::v201809::BiddableAdGroupCriterion->new({
        biddingStrategyConfiguration => $biddingStrategyConfiguration
      });
  } else {
    $criterion = Google::Ads::AdWords::v201809::NegativeAdGroupCriterion->new();
  }

  $criterion->set_adGroupId($ad_group_id);
  $criterion->set_criterion($unit);

  push $operations, create_add_operation($criterion);

  return $unit;
}

# Return a new ADD operation for the specified ad group criterion.
# ad_group_criterion: The ad group criterion for the new operation.
sub create_add_operation {
  my ($ad_group_criterion) = @_;
  my $operation = Google::Ads::AdWords::v201809::AdGroupCriterionOperation->new(
    {
      operand  => $ad_group_criterion,
      operator => "ADD"
    });

  return $operation;
}

# Recursively display a node and each of its children.
# node: The node (Criterion) to display.
# children: Reference to a hash of each criterion ID to the array of its
#   child criteria.
# level: (Optional) The depth of node in the criteria tree.
sub display_tree {
  my ($node, $children, $level) = @_;

  # Recursively display a node and each of its children.
  $level = 0 unless $level;
  my $value = '';
  my $type  = '';

  my $node_dimension = $node->get_caseValue();
  if ($node_dimension) {
    $type = $node_dimension->get_ProductDimension__Type();
    if ($type eq 'ProductCanonicalCondition') {
      $value = $node_dimension->get_condition();
    } elsif ($type eq 'ProductBiddingCategory') {
      $value =
        $node_dimension->get_type() . "(" .
        ($node_dimension->get_value() or '') . ")";
    } elsif ($type eq 'ProductBrand') {
      $value = $node_dimension->get_value();
    } else {
      $value = $node_dimension;
    }
    $value = '' unless $value;
  }

  printf "%sid: %s, type: %s, value: %s\n", "  " x $level,
    $node->get_id(), $type, $value;
  foreach my $child_node (@{$children->{$node->get_id()}}) {
    display_tree($child_node, $children, $level + 1);
  }
}

# Don't run the example if the file is being included.
if (abs_path($0) ne abs_path(__FILE__)) {
  return 1;
}

# Log SOAP XML request, response and API errors.
Google::Ads::AdWords::Logging::enable_all_logging();

# Get AdWords Client, credentials will be read from ~/adwords.properties.
my $client = Google::Ads::AdWords::Client->new({version => "v201809"});

# By default examples are set to die on any server returned fault.
$client->set_die_on_faults(1);

# Call the example
add_product_partition_tree($client, $ad_group_id);

Set a product scope for a campaign

#!/usr/bin/perl -w
#
# Copyright 2017, Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This example restricts the products that will be included in the campaign by
# setting a ProductScope.

use strict;
use lib "../../../lib";
use utf8;

use Data::Uniqid qw(uniqid);

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201809::CampaignCriterion;
use Google::Ads::AdWords::v201809::CampaignCriterionOperation;
use Google::Ads::AdWords::v201809::ProductBiddingCategory;
use Google::Ads::AdWords::v201809::ProductBrand;
use Google::Ads::AdWords::v201809::ProductCanonicalCondition;
use Google::Ads::AdWords::v201809::ProductCustomAttribute;
use Google::Ads::AdWords::v201809::ProductOfferId;
use Google::Ads::AdWords::v201809::ProductScope;
use Google::Ads::AdWords::v201809::ProductType;

use Cwd qw(abs_path);

# Replace with valid values of your account.
my $campaign_id = "INSERT_CAMPAIGN_ID_HERE";

# Example main subroutine.
sub add_product_scope {
  my $client      = shift;
  my $campaign_id = shift;

  my $product_scope = Google::Ads::AdWords::v201809::ProductScope->new({
      # This set of dimensions is for demonstration purposes only. It would be
      # extremely unlikely that you want to include so many dimensions in your
      # product scope.
      dimensions => [
        Google::Ads::AdWords::v201809::ProductBrand->new({value => "Nexus"}),
        Google::Ads::AdWords::v201809::ProductCanonicalCondition->new(
          {condition => "NEW"}
        ),
        Google::Ads::AdWords::v201809::ProductCustomAttribute->new({
            type  => "CUSTOM_ATTRIBUTE_0",
            value => "my attribute value"
          }
        ),
        Google::Ads::AdWords::v201809::ProductOfferId->new({value => "book1"}),
        Google::Ads::AdWords::v201809::ProductType->new({
            type  => "PRODUCT_TYPE_L1",
            value => "Media"
          }
        ),
        Google::Ads::AdWords::v201809::ProductType->new({
            type  => "PRODUCT_TYPE_L2",
            value => "Books"
          }
        ),
        # The value for the bidding category is a fixed ID for the
        # 'Luggage & Bags' category. You can retrieve IDs for categories from the
        # ConstantDataService. See the 'get_product_category_taxonomy' example for
        # more details.
        Google::Ads::AdWords::v201809::ProductBiddingCategory->new({
            type  => "BIDDING_CATEGORY_L1",
            value => -5914235892932915235
          })]});

  my $campaign_criterion =
    Google::Ads::AdWords::v201809::CampaignCriterion->new({
      campaignId => $campaign_id,
      criterion  => $product_scope
    });

  # Create operation.
  my $operation =
    Google::Ads::AdWords::v201809::CampaignCriterionOperation->new({
      operand  => $campaign_criterion,
      operator => "ADD"
    });

  # Make the mutate request.
  my $result =
    $client->CampaignCriterionService()->mutate({operations => [$operation]});

  # Display result.
  $campaign_criterion = $result->get_value()->[0];

  printf("Created a ProductScope criterion with ID '%s'.\n",
    $campaign_criterion->get_criterion()->get_id());

  return 1;
}

# Don't run the example if the file is being included.
if (abs_path($0) ne abs_path(__FILE__)) {
  return 1;
}

# Log SOAP XML request, response and API errors.
Google::Ads::AdWords::Logging::enable_all_logging();

# Get AdWords Client, credentials will be read from ~/adwords.properties.
my $client = Google::Ads::AdWords::Client->new({version => "v201809"});

# By default examples are set to die on any server returned fault.
$client->set_die_on_faults(1);

# Call the example
add_product_scope($client, $campaign_id);

Add a Shopping campaign

#!/usr/bin/perl -w
#
# Copyright 2017, Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This example adds a Shopping campaign.

use strict;
use lib "../../../lib";
use utf8;

use Data::Uniqid qw(uniqid);

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201809::AdGroup;
use Google::Ads::AdWords::v201809::AdGroupAd;
use Google::Ads::AdWords::v201809::AdGroupAdOperation;
use Google::Ads::AdWords::v201809::AdGroupCriterionOperation;
use Google::Ads::AdWords::v201809::AdGroupOperation;
use Google::Ads::AdWords::v201809::BiddableAdGroupCriterion;
use Google::Ads::AdWords::v201809::BiddingStrategyConfiguration;
use Google::Ads::AdWords::v201809::Budget;
use Google::Ads::AdWords::v201809::Campaign;
use Google::Ads::AdWords::v201809::CampaignOperation;
use Google::Ads::AdWords::v201809::CpcBid;
use Google::Ads::AdWords::v201809::ProductAd;
use Google::Ads::AdWords::v201809::ProductPartition;
use Google::Ads::AdWords::v201809::ShoppingSetting;

use Cwd qw(abs_path);

# Replace with valid values of your account.
my $budget_id   = "INSERT_BUDGET_ID_HERE";
my $merchant_id = "INSERT_MERCHANT_CENTER_ID_HERE";
# If set to true (1), a default partition will be created. If running the
# add_product_partition_tree.pl example right after this example,
# make sure this stays set to false (0).
my $create_default_partition = 0;

# Example main subroutine.
sub add_shopping_campaign {
  my ($client, $budget_id, $merchant_id, $create_default_partition) = @_;

  my $campaign = Google::Ads::AdWords::v201809::Campaign->new({
      name => "Shopping campaign #" . uniqid(),
      # The advertisingChannelType is what makes this a Shopping campaign
      advertisingChannelType => "SHOPPING",
      # Recommendation: Set the campaign to PAUSED when creating it to stop
      # the ads from immediately serving. Set to ENABLED once you've added
      # targeting and the ads are ready to serve.
      status => "PAUSED",
      # Set budget (required)
      budget =>
        Google::Ads::AdWords::v201809::Budget->new({budgetId => $budget_id}),
      # Set bidding strategy (required)
      biddingStrategyConfiguration =>
        Google::Ads::AdWords::v201809::BiddingStrategyConfiguration->new(
        {biddingStrategyType => "MANUAL_CPC"}
        ),
      # Set shopping setting (required)
      settings => [
        # All Shopping campaigns need a ShoppingSetting
        Google::Ads::AdWords::v201809::ShoppingSetting->new({
            salesCountry     => "US",
            campaignPriority => 0,
            merchantId       => $merchant_id,
            # By setting enableLocal to true (1) below, you will enable Local
            # Inventory Ads in your campaign. Set this to false (0) if you want
            # to disable this feature in your campaign.
            enableLocal => 1
          })]});

  # Create operation
  my $operation = Google::Ads::AdWords::v201809::CampaignOperation->new({
    operand  => $campaign,
    operator => "ADD"
  });

  # Make the mutate request
  my $result = $client->CampaignService()->mutate({operations => [$operation]});

  # Display result
  $campaign = $result->get_value()->[0];
  printf "Campaign name '%s' and ID %d was added.\n",
    $campaign->get_name(),
    $campaign->get_id();

  # Create ad group
  my $ad_group = Google::Ads::AdWords::v201809::AdGroup->new({
      campaignId => $campaign->get_id(),
      name       => "Ad Group #" . uniqid()});

  # Create operation
  $operation = Google::Ads::AdWords::v201809::AdGroupOperation->new({
    operand  => $ad_group,
    operator => "ADD"
  });

  # Make the mutate request
  $result = $client->AdGroupService()->mutate({operations => [$operation]});

  # Display result
  $ad_group = $result->get_value()->[0];
  printf "Ad group with name '%s' and ID %d was added.\n",
    $ad_group->get_name(),
    $ad_group->get_id();

  # Create product ad
  my $product_ad = Google::Ads::AdWords::v201809::ProductAd->new();

  # Create ad group ad
  my $ad_group_ad = Google::Ads::AdWords::v201809::AdGroupAd->new({
    adGroupId => $ad_group->get_id(),
    ad        => $product_ad
  });

  # Create operation
  $operation = Google::Ads::AdWords::v201809::AdGroupAdOperation->new({
    operand  => $ad_group_ad,
    operator => "ADD"
  });

  # Make the mutate request
  $result = $client->AdGroupAdService()->mutate({operations => [$operation]});

  # Display result
  $ad_group_ad = $result->get_value()->[0];
  printf "Product ad with ID %d was added.\n", $ad_group_ad->get_ad()->get_id();

  if ($create_default_partition) {
    # Create an ad group criterion for 'All products'.
    my $product_partition =
      Google::Ads::AdWords::v201809::ProductPartition->new({
        partitionType => 'UNIT',
        # Make sure the caseValue is null and the parentCriterionId is null.
        caseValue         => undef,
        parentCriterionId => undef
      });

    my $ad_group_criterion =
      Google::Ads::AdWords::v201809::BiddableAdGroupCriterion->new({
        adGroupId => $ad_group->get_id(),
        criterion => $product_partition,
        biddingStrategyConfiguration =>
          Google::Ads::AdWords::v201809::BiddingStrategyConfiguration->new({
            bids => [
              Google::Ads::AdWords::v201809::CpcBid->new({
                  bid => Google::Ads::AdWords::v201809::Money->new(
                    {microAmount => 500000})}
              ),
            ]})});

    # Create operation.
    $operation = Google::Ads::AdWords::v201809::AdGroupCriterionOperation->new({
      operator => "ADD",
      operand  => $ad_group_criterion
    });

    # Make the mutate request
    $result =
      $client->AdGroupCriterionService()->mutate({operations => [$operation]});

    # Display result
    $ad_group_criterion = $result->get_value()->[0];
    printf "Ad group criterion with ID %d in ad group with ID %d was added.\n",
      $ad_group_criterion->get_criterion()->get_id(),
      $ad_group_criterion->get_adGroupId();
  }

  return 1;
}

# Don't run the example if the file is being included.
if (abs_path($0) ne abs_path(__FILE__)) {
  return 1;
}

# Log SOAP XML request, response and API errors.
Google::Ads::AdWords::Logging::enable_all_logging();

# Get AdWords Client, credentials will be read from ~/adwords.properties.
my $client = Google::Ads::AdWords::Client->new({version => "v201809"});

# By default examples are set to die on any server returned fault.
$client->set_die_on_faults(1);

# Call the example
add_shopping_campaign($client, $budget_id, $merchant_id,
  $create_default_partition);

Add a Smart Shopping campaign

#!/usr/bin/perl -w
#
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This example adds a Smart Shopping campaign with an ad group, and ad group
# ad.

use strict;
use lib "../../../lib";
use utf8;

use Data::Uniqid qw(uniqid);

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201809::AdGroup;
use Google::Ads::AdWords::v201809::AdGroupAd;
use Google::Ads::AdWords::v201809::AdGroupAdOperation;
use Google::Ads::AdWords::v201809::AdGroupCriterionOperation;
use Google::Ads::AdWords::v201809::AdGroupOperation;
use Google::Ads::AdWords::v201809::BiddableAdGroupCriterion;
use Google::Ads::AdWords::v201809::Campaign;
use Google::Ads::AdWords::v201809::CampaignOperation;
use Google::Ads::AdWords::v201809::BiddingStrategyConfiguration;
use Google::Ads::AdWords::v201809::Budget;
use Google::Ads::AdWords::v201809::BudgetOperation;
use Google::Ads::AdWords::v201809::GoalOptimizedShoppingAd;
use Google::Ads::AdWords::v201809::Money;
use Google::Ads::AdWords::v201809::ProductPartition;
use Google::Ads::AdWords::v201809::ShoppingSetting;

use Cwd qw(abs_path);

# Replace with valid values of your account.
my $merchant_id = "INSERT_MERCHANT_CENTER_ID_HERE";

# If set to true, a default partition will be created.
# Set it to false if you plan to run the add_product_partition_tree.pl
# example right after this example.
my $should_create_default_partition = 1;

# Example main subroutine.
sub add_smart_shopping_ad {
    my ($client, $merchant_id, $should_create_default_partition) = @_;

    my $budget_id = _create_budget($client);

    my $campaign_id = _create_smart_campaign($client, $budget_id, $merchant_id);

    my $ad_group_id = _create_smart_shopping_ad_group($client, $campaign_id);

    _create_smart_shopping_ad($client, $ad_group_id);

    if (defined($should_create_default_partition)
        && $should_create_default_partition) {
        _create_default_partition($client, $ad_group_id);
    }

    return 1;
}

# Creates a non-shared budget for a Smart Shopping campaign. Smart Shopping
# campaigns support only non-shared budgets.
sub _create_budget() {
    my ($client) = @_;

    my $budget = Google::Ads::AdWords::v201809::Budget->new({
        name => sprintf("Interplanetary Cruise Budget #%s", uniqid()),
        # This budget equals 50.00 units of your account's currency, e.g.,
        # 50 USD if your currency is USD.
        amount =>
            Google::Ads::AdWords::v201809::Money->new({
                microAmount => 50000000
        }),
        deliveryMethod => "STANDARD",
        # Non-shared budgets are required for Smart Shopping campaigns.
        isExplicitlyShared => 0
    });

    my $budget_operation = Google::Ads::AdWords::v201809::BudgetOperation->new({
        operator => "ADD",
        operand  => $budget
    });

    # Add budget.
    my $added_budget =
        $client->BudgetService()->mutate({ operations => ($budget_operation) })
            ->get_value()->[0];

    printf(
        "Budget with name '%s' and ID %d was created.\n",
        $added_budget->get_name(),
        $added_budget->get_budgetId());

    return $added_budget->get_budgetId();
}

# Creates a Smart Shopping campaign.
sub _create_smart_campaign {
    my ($client, $budget_id, $merchant_id) = @_;

    # Create a campaign with required and optional settings.
    my $campaign = Google::Ads::AdWords::v201809::Campaign->new({
        name                         => "Smart Shopping campaign #" . uniqid(),
        # The advertisingChannelType is what makes this a Shopping campaign.
        advertisingChannelType       => "SHOPPING",
        # Sets the advertisingChannelSubType to SHOPPING_GOAL_OPTIMIZED_ADS to
        # make this a Smart Shopping campaign.
        advertisingChannelSubType    => "SHOPPING_GOAL_OPTIMIZED_ADS",
        # Recommendation: Set the campaign to PAUSED when creating it to stop
        # the ads from immediately serving. Set to ENABLED once you've added
        # targeting and the ads are ready to serve.
        status                       => "PAUSED",
        # Set budget (required)
        budget                       =>
            Google::Ads::AdWords::v201809::Budget
                ->new({ budgetId => $budget_id }),
        # Set a bidding strategy. Only MAXIMIZE_CONVERSION_VALUE is supported.
        biddingStrategyConfiguration =>
            Google::Ads::AdWords::v201809::BiddingStrategyConfiguration->new(
                { biddingStrategyType => "MAXIMIZE_CONVERSION_VALUE" }
            ),
        # All Shopping campaigns need a ShoppingSetting.
        settings                     => [
            Google::Ads::AdWords::v201809::ShoppingSetting->new({
                salesCountry => "US",
                merchantId   => $merchant_id
            }) ] });

    my $operation = Google::Ads::AdWords::v201809::CampaignOperation->new({
        operand  => $campaign,
        operator => "ADD"
    });

    # Create the campaign on the server and print out some information.
    my $added_campaign = $client->CampaignService()
        ->mutate({ operations => [ $operation ] })
        ->get_value()->[0];
    printf(
        "Smart Shopping campaign with name '%s' and ID %d was added.\n",
        $added_campaign->get_name(),
        $added_campaign->get_id());

    return $added_campaign->get_id();
}

# Creates a Smart Shopping ad group by setting the ad group type to
# SHOPPING_GOAL_OPTIMIZED_ADS.
sub _create_smart_shopping_ad_group {
    my ($client, $campaign_id) = @_;

    # Create an ad group.
    my $ad_group = Google::Ads::AdWords::v201809::AdGroup->new({
        campaignId  => $campaign_id,
        name        => "Smart Shopping ad group #" . uniqid(),
        # Set the ad group type to SHOPPING_GOAL_OPTIMIZED_ADS.
        adGroupType => "SHOPPING_GOAL_OPTIMIZED_ADS"
    });

    # Create operation.
    my $operation = Google::Ads::AdWords::v201809::AdGroupOperation->new({
        operand  => $ad_group,
        operator => "ADD"
    });

    # Create the ad group on the server and print out some information.
    my $added_ad_group = $client->AdGroupService()
        ->mutate({ operations => [ $operation ] })
        ->get_value()->[0];

    printf(
        "Smart Shopping ad group with name '%s' and ID %d was added.\n",
        $added_ad_group->get_name(),
        $added_ad_group->get_id()
    );
    return $added_ad_group->get_id();
}

# Creates a Smart Shopping ad.
sub _create_smart_shopping_ad {
    my ($client, $ad_group_id) = @_;

    # Create a Smart Shopping ad (Goal-optimized Shopping ad).
    my $smart_shopping_ad
        = Google::Ads::AdWords::v201809::GoalOptimizedShoppingAd->new({ });

    # Create ad group ad.
    my $ad_group_ad = Google::Ads::AdWords::v201809::AdGroupAd->new({
        adGroupId => $ad_group_id,
        ad        => $smart_shopping_ad
    });

    # Create an ad group ad operation and add it to the operations list.
    my $operation = Google::Ads::AdWords::v201809::AdGroupAdOperation->new({
        operand  => $ad_group_ad,
        operator => "ADD"
    });

    # Create the ad group ad on the server and print out some information.
    my $added_ad_group_ad =
        $client->AdGroupAdService()->mutate({ operations => [ $operation ] })
            ->get_value()->[0];
    printf(
        "Smart Shopping ad with ID %d was added.\n",
        $added_ad_group_ad->get_ad()->get_id()
    );
}

# Creates a default product partition as an ad group criterion.
sub _create_default_partition {
    my ($client, $ad_group_id) = @_;

    # Creates an ad group criterion for 'All products'.
    my $product_partition = Google::Ads::AdWords::v201809::ProductPartition
        ->new({ partitionType => "UNIT" });

    # Creates a biddable ad group criterion.
    my $criterion =
        Google::Ads::AdWords::v201809::BiddableAdGroupCriterion->new({
            adGroupId => $ad_group_id,
            criterion => $product_partition
        });

    # Creates an ad group criterion operation.
    my $operation = Google::Ads::AdWords::v201809::AdGroupCriterionOperation
        ->new({
        operand  => $criterion,
        operator => "ADD"
    });

    my $added_ad_group_criterion = $client->AdGroupCriterionService()
        ->mutate({ operations => [ $operation ] })
        ->get_value()->[0];
    printf(
        "Ad group criterion with ID %d in ad group with ID %d"
            . " was added.\n",
        $added_ad_group_criterion->get_criterion()->get_id(),
        $added_ad_group_criterion->get_adGroupId()
    );
}

# Log SOAP XML request, response and API errors.
Google::Ads::AdWords::Logging::enable_all_logging();

# Get AdWords Client, credentials will be read from ~/adwords.properties.
my $client = Google::Ads::AdWords::Client->new({ version => "v201809" });

# By default examples are set to die on any server returned fault.
$client->set_die_on_faults(1);

# Call the example
add_smart_shopping_ad($client, $merchant_id, $should_create_default_partition);

Get the set of product bidding categories

#!/usr/bin/perl -w
#
# Copyright 2017, Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This example fetches the set of valid ProductBiddingCategories.

use strict;
use lib "../../../lib";
use utf8;

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201809::Selector;
use Google::Ads::AdWords::v201809::Predicate;

use Cwd qw(abs_path);

# Example main subroutine.
sub get_product_category_taxonomy {
  my $client = shift;

  # Create selector.
  my $selector = Google::Ads::AdWords::v201809::Selector->new({
      fields     => ["DimensionValue", "ParentDimensionValue", "DisplayValue"],
      predicates => [
        Google::Ads::AdWords::v201809::Predicate->new({
            field    => "Country",
            operator => "IN",
            values   => ["US"]})]});

  my $results =
    $client->ConstantDataService()
    ->getProductBiddingCategoryData({selector => $selector});

  if ($results) {
    # %bidding_categories is a hash where key=dimension ID
    # and value=a hash of properties
    my %bidding_categories = ();
    my @root_categories    = ();
    foreach my $product_bidding_category_data (@{$results}) {
      my $id =
        $product_bidding_category_data->get_dimensionValue()->get_value();
      my $parent_id;
      my $name =
        $product_bidding_category_data->get_displayValue()->[0]->get_value();
      if ($product_bidding_category_data->get_parentDimensionValue()) {
        $parent_id =
          $product_bidding_category_data->get_parentDimensionValue()
          ->get_value();
      }

      if (!(exists $bidding_categories{$id})) {
        $bidding_categories{$id} = {};
      }

      my $category = $bidding_categories{$id};

      if ($parent_id) {
        if (!(exists $bidding_categories{$parent_id})) {
          $bidding_categories{$parent_id} = {};
        }
        my $parent = $bidding_categories{$parent_id};

        if (!(exists $parent->{"children"})) {
          $parent->{"children"} = [];
        }
        my $children = $parent->{"children"};
        push $children, $category;
      } else {
        push @root_categories, $category;
      }

      $category->{"id"}   = $id;
      $category->{"name"} = $name;
    }
    display_categories(\@root_categories, "");
  } else {
    print "No product bidding category data items were found.\n";
  }

  return 1;
}

sub display_categories {
  my $categories = shift;
  my $prefix     = shift;
  foreach my $category (@{$categories}) {
    printf "%s%s [%s]\n", $prefix, $category->{"name"}, $category->{"id"};
    if (exists $category->{"children"}) {
      my $category_name = $category->{"name"};
      my $children      = $category->{"children"};
      display_categories($children, "${prefix}${category_name} > ");
    }
  }
}

# Don't run the example if the file is being included.
if (abs_path($0) ne abs_path(__FILE__)) {
  return 1;
}

# Log SOAP XML request, response and API errors.
Google::Ads::AdWords::Logging::enable_all_logging();

# Get AdWords Client, credentials will be read from ~/adwords.properties.
my $client = Google::Ads::AdWords::Client->new({version => "v201809"});

# By default examples are set to die on any server returned fault.
$client->set_die_on_faults(1);

# Call the example
get_product_category_taxonomy($client);