Basic Operations Samples

The code samples below provide examples of basic operations using the AdWords API. Client Library.

Add ad groups

#!/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 ad groups to a campaign. To get campaigns, run
# get_all_campaigns.pl.

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

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201710::AdGroup;
use Google::Ads::AdWords::v201710::AdGroupAdRotationMode;
use Google::Ads::AdWords::v201710::AdGroupOperation;
use Google::Ads::AdWords::v201710::BiddingStrategyConfiguration;
use Google::Ads::AdWords::v201710::CpcBid;
use Google::Ads::AdWords::v201710::Money;
use Google::Ads::AdWords::v201710::TargetingSetting;
use Google::Ads::AdWords::v201710::TargetingSettingDetail;

use Cwd qw(abs_path);
use Data::Uniqid qw(uniqid);

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

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

  my $num_ad_groups = 2;
  my @operations    = ();
  for (my $i = 0 ; $i < $num_ad_groups ; $i++) {
    # Create ad group.
    my $ad_group = Google::Ads::AdWords::v201710::AdGroup->new({
        name       => "Earth to Mars Cruises #" . uniqid(),
        campaignId => $campaign_id,
        biddingStrategyConfiguration =>
          Google::Ads::AdWords::v201710::BiddingStrategyConfiguration->new({
            bids => [
              Google::Ads::AdWords::v201710::CpcBid->new({
                  bid => Google::Ads::AdWords::v201710::Money->new({
                    microAmount => 1000000
                  }),
                }
              ),
            ]}
          ),
        # Additional properties (non-required).
        status => "ENABLED",
        # Set the rotation mode.
        adGroupAdRotationMode =>
          Google::Ads::AdWords::v201710::AdGroupAdRotationMode->new({
            adRotationMode => 'OPTIMIZE'
          }),
        settings => [
          # Targeting restriction settings. Depending on the
          # criterionTypeGroup value, most TargetingSettingDetail only
          # affect Display campaigns. However, the USER_INTEREST_AND_LIST
          # value works for RLSA campaigns - Search campaigns targeting
          # using a remarketing list.
          Google::Ads::AdWords::v201710::TargetingSetting->new({
              details => [
                # Restricting to serve ads that match your ad group placements.
                # This is equivalent to choosing "Target and bid" in the UI.
                Google::Ads::AdWords::v201710::TargetingSettingDetail->new({
                    criterionTypeGroup => "PLACEMENT",
                    targetAll          => 0
                  }
                ),
                # Using your ad group verticals only for bidding. This is equivalent
                # to choosing "Bid only" in the UI.
                Google::Ads::AdWords::v201710::TargetingSettingDetail->new({
                    criterionTypeGroup => "VERTICAL",
                    targetAll          => 1
                })
              ]
          })
        ]
    });
    # Create operation.
    my $ad_group_operation =
      Google::Ads::AdWords::v201710::AdGroupOperation->new({
        operator => "ADD",
        operand  => $ad_group
      });
    push @operations, $ad_group_operation;
  }

  # Add ad groups.
  my $result = $client->AdGroupService()->mutate({operations => \@operations});

  # Display ad groups.
  foreach my $ad_group (@{$result->get_value()}) {
    printf "Ad group with name \"%s\" and id \"%d\" was added.\n",
      $ad_group->get_name(), $ad_group->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 => "v201710"});

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

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

Add campaigns

#!/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 campaigns.

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

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201710::BiddingStrategyConfiguration;
use Google::Ads::AdWords::v201710::Budget;
use Google::Ads::AdWords::v201710::BudgetOperation;
use Google::Ads::AdWords::v201710::Campaign;
use Google::Ads::AdWords::v201710::CampaignOperation;
use Google::Ads::AdWords::v201710::FrequencyCap;
use Google::Ads::AdWords::v201710::GeoTargetTypeSetting;
use Google::Ads::AdWords::v201710::ManualCpcBiddingScheme;
use Google::Ads::AdWords::v201710::Money;
use Google::Ads::AdWords::v201710::NetworkSetting;

use Cwd qw(abs_path);
use Data::Uniqid qw(uniqid);

sub add_campaigns {
  my $client = shift;

  # Create a budget, which can be shared by multiple campaigns.
  my $budget = Google::Ads::AdWords::v201710::Budget->new({
      # Required attributes.
      name => "Interplanetary budget #" . uniqid(),
      amount =>
        Google::Ads::AdWords::v201710::Money->new({microAmount => 5000000}),
      deliveryMethod => "STANDARD"
  });

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

  # Add budget.
  my $budgetId =
    $client->BudgetService()->mutate({operations => ($budget_operation)})
    ->get_value()->get_budgetId()->get_value();
  # Create campaigns.
  my $num_campaigns = 2;
  my @operations    = ();
  for (my $i = 0 ; $i < $num_campaigns ; $i++) {
    my (undef, undef, undef, $mday, $mon, $year) = localtime(time);
    my $today = sprintf("%d%02d%02d", ($year + 1900), ($mon + 1), $mday);
    (undef, undef, undef, $mday, $mon, $year) = localtime(time + 60 * 60 * 24);
    my $tomorrow = sprintf("%d%02d%02d", ($year + 1900), ($mon + 1), $mday);
    my $campaign = Google::Ads::AdWords::v201710::Campaign->new({
        name => "Interplanetary Cruise #" . uniqid(),
        # Bidding strategy (required).
        biddingStrategyConfiguration =>
          Google::Ads::AdWords::v201710::BiddingStrategyConfiguration->new({
            biddingStrategyType => "MANUAL_CPC",
            # You can optionally provide a bidding scheme in place of the type.
          }
          ),
        # Budget (required) - note only the budgetId is required.
        budget =>
          Google::Ads::AdWords::v201710::Budget->new({budgetId => $budgetId}),
        # Create a Search Network with Display Select campaign.
        # To create a Display Only campaign, omit networkSetting and use the
        # DISPLAY advertisingChannelType.
        # NetworkSetting (optional).
        networkSetting => Google::Ads::AdWords::v201710::NetworkSetting->new({
            targetGoogleSearch         => 1,
            targetSearchNetwork        => 1,
            targetContentNetwork       => 1,
            targetPartnerSearchNetwork => 0
          }
        ),
        # Advertising channel type (required).
        advertisingChannelType => "SEARCH",
        # Frequency cap (non-required).
        frequencyCap => Google::Ads::AdWords::v201710::FrequencyCap->new({
            impressions => 5,
            timeUnit    => "DAY",
            level       => "ADGROUP"
          }
        ),
        settings => [
          # Advanced location targeting settings (non-required).
          Google::Ads::AdWords::v201710::GeoTargetTypeSetting->new({
              positiveGeoTargetType => "DONT_CARE",
              negativeGeoTargetType => "DONT_CARE"
            }
          ),
        ],
        # Additional properties (non-required).
        startDate                   => $today,
        endDate                     => $tomorrow,
        # 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"
      });

    # Create operation.
    my $campaign_operation =
      Google::Ads::AdWords::v201710::CampaignOperation->new({
        operator => "ADD",
        operand  => $campaign
      });
    push @operations, $campaign_operation;
  }

  # Add campaigns.
  my $result = $client->CampaignService()->mutate({operations => \@operations});

  # Display campaigns.
  foreach my $campaign (@{$result->get_value()}) {
    printf "Campaign with name \"%s\" and id \"%s\" was added.\n",
      $campaign->get_name(), $campaign->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 => "v201710"});

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

# Call the example
add_campaigns($client);

Add expanded text ads

#!/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 code example adds expanded text ads to a given ad group.
# To get ad groups, run get_ad_groups.pl.

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

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201710::AdGroupAd;
use Google::Ads::AdWords::v201710::AdGroupAdOperation;
use Google::Ads::AdWords::v201710::ExpandedTextAd;

use Cwd qw(abs_path);
use Data::Uniqid qw(uniqid);

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

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

  my $num_ads    = 5;
  my @operations = ();
  for (my $i = 0 ; $i < $num_ads ; $i++) {
    # Create text ad.
    my $expanded_text_ad = Google::Ads::AdWords::v201710::ExpandedTextAd->new({
        headlinePart1 => "Cruise to Mars #" . substr(uniqid(), 0, 8),
        headlinePart2 => "Best Space Cruise Line",
        description   => "Buy your tickets now!",
        finalUrls     => ["http://www.example.com/" . $i],
        path1         => "all-inclusive",
        path2         => "deals"
    });

    # Create ad group ad for the expanded text ad.
    my $ad_group_ad = Google::Ads::AdWords::v201710::AdGroupAd->new({
        adGroupId => $ad_group_id,
        ad        => $expanded_text_ad,
        # Additional properties (non-required).
        status => "PAUSED"
    });

    # Create operation.
    my $ad_group_ad_operation =
      Google::Ads::AdWords::v201710::AdGroupAdOperation->new({
        operator => "ADD",
        operand  => $ad_group_ad
      });
    push @operations, $ad_group_ad_operation;
  }

  # Add expanded text ad.
  my $result =
    $client->AdGroupAdService()->mutate({operations => \@operations});

  # Display results.
  if ($result->get_value()) {
    foreach my $ad_group_ad (@{$result->get_value()}) {
      printf "New expanded text ad with id \"%d\" and " .
        "headline \"%s - %s\" was added.\n",
        $ad_group_ad->get_ad()->get_id(),
        $ad_group_ad->get_ad()->get_headlinePart1(),
        $ad_group_ad->get_ad()->get_headlinePart2();
    }
  } else {
    print "No expanded text ads were added.\n";
  }

  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 => "v201710"});

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

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

Add keywords

#!/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 multiple keywords to an ad group. To get ad groups run
# basic_operations/get_ad_groups.pl.

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

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201710::AdGroupCriterionOperation;
use Google::Ads::AdWords::v201710::BiddableAdGroupCriterion;
use Google::Ads::AdWords::v201710::BiddingStrategyConfiguration;
use Google::Ads::AdWords::v201710::CpcBid;
use Google::Ads::AdWords::v201710::Keyword;
use Google::Ads::AdWords::v201710::Money;
use Google::Ads::AdWords::v201710::Placement;
use Google::Ads::AdWords::v201710::UrlList;

use Cwd qw(abs_path);
use Data::Uniqid qw(uniqid);
use URI::Escape qw(uri_escape);

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

# Example main subroutine.
sub add_keywords {
  my $client      = shift;
  my $ad_group_id = shift;
  # Create keywords.
  my @keywords = ("mars cruise", "space hotel");

  # Create operations.
  my @operations = ();
  foreach my $keyword_text (@keywords) {
    my $keyword = Google::Ads::AdWords::v201710::Keyword->new({
        text      => $keyword_text,
        matchType => "BROAD"
    });

    # Create biddable ad group criterion.
    my $final_url =
      'http://www.example.com/mars/cruise/?kw=' . uri_escape($keyword_text);
    my $keyword_biddable_ad_group_criterion =
      Google::Ads::AdWords::v201710::BiddableAdGroupCriterion->new({
        adGroupId => $ad_group_id,
        criterion => $keyword,
        # Set bids (non-required).
        biddingStrategyConfiguration =>
          Google::Ads::AdWords::v201710::BiddingStrategyConfiguration->new({
            bids => [
              Google::Ads::AdWords::v201710::CpcBid->new({
                  bid => Google::Ads::AdWords::v201710::Money->new(
                    {microAmount => 500000})}
              ),
            ]}
          ),
        # Additional properties (non-required).
        userStatus => "PAUSED",
        finalUrls =>
          [Google::Ads::AdWords::v201710::UrlList->new({urls => [$final_url]})]}
      );

    # Create operation.
    my $keyword_ad_group_operation =
      Google::Ads::AdWords::v201710::AdGroupCriterionOperation->new({
        operator => "ADD",
        operand  => $keyword_biddable_ad_group_criterion
      });
    push @operations, $keyword_ad_group_operation;
  }

  # Add ad group criteria.
  my $result =
    $client->AdGroupCriterionService()->mutate({operations => \@operations});

  # Display ad group criteria.
  if ($result->get_value()) {
    foreach my $keyword (@{$result->get_value()}) {
      printf "Keyword with ad group id \"%d\", id \"%d\", " .
        "text \"%s\" and match type \"%s\" was added.\n",
        $keyword->get_adGroupId(),
        $keyword->get_criterion()->get_id(),
        $keyword->get_criterion()->get_text(),
        $keyword->get_criterion()->get_matchType();
    }
  } else {
    print "No keywords were added.";
  }

  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 => "v201710"});

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

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

Get ad groups

#!/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 gets all ad groups in a campaign. To add ad groups, run
# basic_operations/add_ad_group.pl. To get campaigns, run
# basic_operations/get_campaigns.pl.

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

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201710::OrderBy;
use Google::Ads::AdWords::v201710::Paging;
use Google::Ads::AdWords::v201710::Predicate;
use Google::Ads::AdWords::v201710::Selector;
use Google::Ads::AdWords::Utilities::PageProcessor;

use constant PAGE_SIZE => 500;

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

use Cwd qw(abs_path);

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

  # Create predicates.
  my $campaign_predicate = Google::Ads::AdWords::v201710::Predicate->new({
      field    => "CampaignId",
      operator => "IN",
      values   => [$campaign_id]});

  # Create selector.
  my $paging = Google::Ads::AdWords::v201710::Paging->new({
      startIndex    => 0,
      numberResults => PAGE_SIZE
  });
  my $selector = Google::Ads::AdWords::v201710::Selector->new({
      fields     => ["Id", "Name"],
      predicates => [$campaign_predicate],
      ordering   => [
        Google::Ads::AdWords::v201710::OrderBy->new({
            field     => "Name",
            sortOrder => "ASCENDING"
          })
      ],
      paging => $paging
    });

  # Paginate through results.
  # The contents of the subroutine will be executed for each ad group.
  Google::Ads::AdWords::Utilities::PageProcessor->new({
      client   => $client,
      service  => $client->AdGroupService(),
      selector => $selector
    }
    )->process_entries(
    sub {
      my ($ad_group) = @_;
      printf "Ad group with name \"%s\" and id \"%d\" was found.\n",
        $ad_group->get_name(), $ad_group->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 => "v201710"});

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

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

Get campaigns

#!/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 gets all campaigns. To add a campaign, run
# basic_operations/add_campaign.pl.

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

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201710::OrderBy;
use Google::Ads::AdWords::v201710::Paging;
use Google::Ads::AdWords::v201710::Predicate;
use Google::Ads::AdWords::v201710::Selector;
use Google::Ads::AdWords::Utilities::PageProcessor;

use constant PAGE_SIZE => 500;

use Cwd qw(abs_path);

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

  # Create selector.
  my $paging = Google::Ads::AdWords::v201710::Paging->new({
      startIndex    => 0,
      numberResults => PAGE_SIZE
  });
  my $selector = Google::Ads::AdWords::v201710::Selector->new({
      fields   => ["Id", "Name"],
      ordering => [
        Google::Ads::AdWords::v201710::OrderBy->new({
            field     => "Name",
            sortOrder => "ASCENDING"
          })
      ],
      paging => $paging
    });

  # Paginate through results.
  # The contents of the subroutine will be executed for each campaign.
  Google::Ads::AdWords::Utilities::PageProcessor->new({
      client   => $client,
      service  => $client->CampaignService(),
      selector => $selector
    }
    )->process_entries(
    sub {
      my ($campaign) = @_;
      printf "Campaign with name \"%s\" and id \"%d\" was found.\n",
        $campaign->get_name(), $campaign->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 => "v201710"});

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

# Call the example
get_campaigns($client);

Get campaigns with AWQL

#!/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 gets all campaigns using AWQL. To add a campaign, run
# basic_operations/add_campaign.pl.

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

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201710::OrderBy;
use Google::Ads::AdWords::v201710::Paging;
use Google::Ads::AdWords::v201710::Predicate;
use Google::Ads::AdWords::v201710::Selector;
use Google::Ads::AdWords::Utilities::PageProcessor;

use constant PAGE_SIZE => 500;

use Cwd qw(abs_path);

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

  # Get all the campaigns for this account.
  my $query = "SELECT Id, Name, Status ORDER BY Name";

  # Paginate through results.
  # The contents of the subroutine will be executed for each campaign.
  Google::Ads::AdWords::Utilities::PageProcessor->new({
      client    => $client,
      service   => $client->CampaignService(),
      query     => $query,
      page_size => PAGE_SIZE
    }
    )->process_entries(
    sub {
      my ($campaign) = @_;
      printf "Campaign with name \"%s\" and id \"%d\" was found.\n",
        $campaign->get_name(), $campaign->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 => "v201710"});

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

# Call the example
get_campaigns_with_awql($client);

Get expanded text ads

#!/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 gets non-removed expanded text ads in an ad group. To add
# expanded text ads, run add_expanded_text_ads.pl.
# To get ad groups, run get_ad_groups.pl.

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

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201710::OrderBy;
use Google::Ads::AdWords::v201710::Paging;
use Google::Ads::AdWords::v201710::Predicate;
use Google::Ads::AdWords::v201710::Selector;
use Google::Ads::AdWords::Utilities::PageProcessor;

use constant PAGE_SIZE => 500;

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 get_expanded_text_ads {
  my $client      = shift;
  my $ad_group_id = shift;

  # Create predicates.
  my $ad_group_predicate = Google::Ads::AdWords::v201710::Predicate->new({
      field    => "AdGroupId",
      operator => "IN",
      values   => [$ad_group_id]});
  my $status_predicate = Google::Ads::AdWords::v201710::Predicate->new({
      field    => "Status",
      operator => "IN",
      values   => ["ENABLED", "PAUSED"]});
  my $ad_type_predicate = Google::Ads::AdWords::v201710::Predicate->new({
      field    => "AdType",
      operator => "EQUALS",
      values   => ["EXPANDED_TEXT_AD"]});

  # Create selector.
  my $paging = Google::Ads::AdWords::v201710::Paging->new({
      startIndex    => 0,
      numberResults => PAGE_SIZE
  });
  my $selector = Google::Ads::AdWords::v201710::Selector->new({
      fields =>
        ["Id", "Status", "HeadlinePart1", "HeadlinePart2", "Description"],
      predicates =>
        [$ad_group_predicate, $status_predicate, $ad_type_predicate],
      ordering => [
        Google::Ads::AdWords::v201710::OrderBy->new({
            field     => "Id",
            sortOrder => "ASCENDING"
          })
      ],
      paging => $paging
    });

  # Paginate through results.
  # The contents of the subroutine will be executed for each text ad.
  Google::Ads::AdWords::Utilities::PageProcessor->new({
      client   => $client,
      service  => $client->AdGroupAdService(),
      selector => $selector
    }
    )->process_entries(
    sub {
      my ($ad_group_ad) = @_;
      printf "Expanded text ad with id \"%d\", status \"%s\", and " .
        "headline \"%s - %s\" was found.\n",
        $ad_group_ad->get_ad()->get_id(),
        $ad_group_ad->get_status(),
        $ad_group_ad->get_ad()->get_headlinePart1(),
        $ad_group_ad->get_ad()->get_headlinePart2();
    });

  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 => "v201710"});

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

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

Get keywords

#!/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 gets keywords in an ad group. To add keywords, run
# basic_operations/add_keywords.pl. To get ad groups, run
# basic_operations/get_ad_groups.pl.

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

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201710::OrderBy;
use Google::Ads::AdWords::v201710::Paging;
use Google::Ads::AdWords::v201710::Predicate;
use Google::Ads::AdWords::v201710::Selector;
use Google::Ads::AdWords::Utilities::PageProcessor;

use Cwd qw(abs_path);

use constant PAGE_SIZE => 500;

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

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

  # Create selector.
  my $ad_group_id_predicate = Google::Ads::AdWords::v201710::Predicate->new({
      field    => "AdGroupId",
      operator => "IN",
      values   => [$ad_group_id]});
  my $criteria_type_predicate = Google::Ads::AdWords::v201710::Predicate->new({
      field    => "CriteriaType",
      operator => "EQUALS",
      values   => ["KEYWORD"]});
  my $paging = Google::Ads::AdWords::v201710::Paging->new({
      startIndex    => 0,
      numberResults => PAGE_SIZE
  });
  my $selector = Google::Ads::AdWords::v201710::Selector->new({
      fields => ["Id", "CriteriaType", "KeywordMatchType", "KeywordText"],
      predicates => [$ad_group_id_predicate, $criteria_type_predicate],
      ordering => Google::Ads::AdWords::v201710::OrderBy->new({
          field     => "KeywordText",
          sortOrder => "ASCENDING"
        }
      ),
      paging => $paging
    });

  # Paginate through results.
  # The contents of the subroutine will be executed for each criterion.
  Google::Ads::AdWords::Utilities::PageProcessor->new({
      client   => $client,
      service  => $client->AdGroupCriterionService(),
      selector => $selector
    }
    )->process_entries(
    sub {
      my ($ad_group_criterion) = @_;
      my $prefix = "Keyword";
      if (
        $ad_group_criterion->isa(
          "Google::Ads::AdWords::v201710::NegativeAdGroupCriterion"))
      {
        my $prefix = "Negative keyword";
      }
      printf "$prefix with text '%s', match type '%s', criteria type '%s', "
        . "and ID %d was found.\n",
        $ad_group_criterion->get_criterion()->get_text(),
        $ad_group_criterion->get_criterion()->get_matchType(),
        $ad_group_criterion->get_criterion()->get_type(),
        $ad_group_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 => "v201710"});

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

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

Pause an ad

#!/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 updates an ad by setting the status to 'PAUSED'. To get ads,
# run basic_operations/get_expanded_text_ads.pl.

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

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201710::AdGroupAd;
use Google::Ads::AdWords::v201710::AdGroupAdOperation;

use Cwd qw(abs_path);

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

# Example main subroutine.
sub pause_ad {
  my $client      = shift;
  my $ad_group_id = shift;
  my $ad_id       = shift;

  # Create ad with updated status.
  my $ad = Google::Ads::AdWords::v201710::Ad->new({id => $ad_id});

  my $ad_group_ad = Google::Ads::AdWords::v201710::AdGroupAd->new({
      adGroupId => $ad_group_id,
      ad        => $ad,
      status    => "PAUSED"
  });

  # Create operation.
  my $operation = Google::Ads::AdWords::v201710::AdGroupAdOperation->new({
      operand  => $ad_group_ad,
      operator => "SET"
  });

  # Validate the ads.
  my $result =
    $client->AdGroupAdService()->mutate({operations => [$operation]});

  # Reading the response.
  if ($result->get_value()) {
    my $ad_group_ad = $result->get_value()->[0];
    printf "Ad with id \"%d\", type \"%s\" and status \"%s\" was updated.\n",
      $ad_group_ad->get_ad()->get_id(),
      $ad_group_ad->get_ad()->get_Ad__Type(), $ad_group_ad->get_status();
  } else {
    print "No ad was updated.";
  }

  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 => "v201710"});

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

# Call the example
pause_ad($client, $ad_group_id, $ad_id);

Remove an ad

#!/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 removes an ad using the 'REMOVE' operator. To get ads, run
# basic_operations/get_expanded_text_ads.pl.

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

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201710::Ad;
use Google::Ads::AdWords::v201710::AdGroupAd;
use Google::Ads::AdWords::v201710::AdGroupAdOperation;

use Cwd qw(abs_path);

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

# Example main subroutine.
sub remove_ad {
  my $client      = shift;
  my $ad_group_id = shift;
  my $ad_id       = shift;

  # Create base class ad to avoid setting type specific fields.
  my $ad = Google::Ads::AdWords::v201710::Ad->new({id => $ad_id,});

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

  # Create operations.
  my $operation = Google::Ads::AdWords::v201710::AdGroupAdOperation->new({
      operand  => $ad_group_ad,
      operator => "REMOVE"
  });

  # Remove ad.
  my $result =
    $client->AdGroupAdService()->mutate({operations => [$operation]});

  # Display ads.
  if ($result->get_value()) {
    my $ad_group_ad = $result->get_value()->[0];
    printf "Ad with id \"%d\" and type \"%s\" was removed.\n",
      $ad_group_ad->get_ad()->get_id(),
      $ad_group_ad->get_ad()->get_Ad__Type();
  } else {
    print "No ad was removed.\n";
  }

  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 => "v201710"});

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

# Call the example
remove_ad($client, $ad_group_id, $ad_id);

Remove 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 removes an ad group by setting the status to 'REMOVED'.
# To get ad groups, run basic_operations/get_ad_groups.pl.

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

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201710::AdGroup;
use Google::Ads::AdWords::v201710::AdGroupOperation;

use Cwd qw(abs_path);
use Data::Uniqid qw(uniqid);

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

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

  # Create ad group with REMOVED status.
  my $ad_group = Google::Ads::AdWords::v201710::AdGroup->new({
      id     => $ad_group_id,
      status => "REMOVED"
  });

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

  # Remove ad group.
  my $result = $client->AdGroupService()->mutate({operations => [$operation]});

  # Display ad groups.
  if ($result->get_value()) {
    my $ad_group = $result->get_value()->[0];
    printf "The ad group with id %d was removed.\n", $ad_group->get_id();
  } else {
    print "No ad group was removed.\n";
  }

  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 => "v201710"});

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

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

Remove 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 removes a campaign by setting the status to 'REMOVED'.
# To get campaigns, run basic_operations/get_campaigns.pl.

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

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201710::Campaign;
use Google::Ads::AdWords::v201710::CampaignOperation;

use Cwd qw(abs_path);
use Data::Uniqid qw(uniqid);

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

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

  # Create campaign with REMOVED status.
  my $campaign = Google::Ads::AdWords::v201710::Campaign->new({
      id     => $campaign_id,
      status => "REMOVED"
  });

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

  # Remove campaign.
  my $result = $client->CampaignService()->mutate({operations => [$operation]});

  # Display campaign.
  if ($result->get_value()) {
    my $campaign = $result->get_value()->[0];
    printf "The campaign with id %d was removed.\n", $campaign->get_id();
  } else {
    print "No campaign was removed.\n";
  }

  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 => "v201710"});

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

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

Remove a keyword

#!/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 removes a keyword using the 'REMOVE' operator. To get keywords,
# run basic_operations/get_keywords.pl.

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

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201710::AdGroupCriterion;
use Google::Ads::AdWords::v201710::AdGroupCriterionOperation;

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

use Cwd qw(abs_path);

# Example main subroutine.
sub remove_keyword {
  my $client      = shift;
  my $ad_group_id = shift;
  my $keyword_id  = shift;

  # Create base class criterion to avoid setting keyword specific fields.
  my $criterion =
    Google::Ads::AdWords::v201710::Criterion->new({id => $keyword_id});

  # Create ad group criterion.
  my $ad_group_criterion = Google::Ads::AdWords::v201710::AdGroupCriterion->new(
    {
      adGroupId => $ad_group_id,
      criterion => $criterion
    });

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

  # Remove ad group criteria.
  my $result =
    $client->AdGroupCriterionService()->mutate({operations => [$operation]});

  # Display ad group criteria.
  if ($result->get_value()) {
    my $ad_group_criterion = $result->get_value()->[0];
    printf "Keyword with ad group id \"%d\" and id \"%d\" was removed.\n",
      $ad_group_criterion->get_adGroupId(),
      $ad_group_criterion->get_criterion()->get_id();
  } else {
    print "No keyword was removed.\n";
  }

  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 => "v201710"});

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

# Call the example
remove_keyword($client, $ad_group_id, $keyword_id);

Update 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 updates an ad group by setting the status to 'PAUSED'.
# To get ad groups, run basic_operations/get_ad_groups.pl.

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

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201710::AdGroup;
use Google::Ads::AdWords::v201710::AdGroupOperation;

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 update_ad_group {
  my $client      = shift;
  my $ad_group_id = shift;

  # Create ad group with updated status.
  my $ad_group = Google::Ads::AdWords::v201710::AdGroup->new({
      id     => $ad_group_id,
      status => "PAUSED"
  });

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

  # Update ad group.
  my $result = $client->AdGroupService()->mutate({operations => [$operation]});

  # Display ad groups.
  if ($result->get_value()) {
    my $ad_group = $result->get_value()->[0];
    printf "Ad group with name \"%s\", id \"%d\", and status \"%s\" was " .
      "updated.\n", $ad_group->get_name(), $ad_group->get_id(),
      $ad_group->get_status();
  } else {
    print "No ad group was updated.\n";
  }

  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 => "v201710"});

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

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

Update 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 updates a campaign by setting its status to PAUSED.
# To get campaigns, run basic_operations/get_campaigns.pl.

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

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201710::Campaign;
use Google::Ads::AdWords::v201710::CampaignOperation;

use Cwd qw(abs_path);

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

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

  # Create campaign with updated status.
  my $campaign = Google::Ads::AdWords::v201710::Campaign->new({
      id     => $campaign_id,
      status => "PAUSED"
  });

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

  # Update campaign.
  my $result = $client->CampaignService()->mutate({operations => [$operation]});

  # Display campaigns.
  if ($result->get_value()) {
    my $campaign = $result->get_value()->[0];
    printf "Campaign with name \"%s\", id \"%d\" and status " .
      "\"%s\" was updated.\n", $campaign->get_name(), $campaign->get_id(),
      $campaign->get_status();
  } else {
    print "No campaign was updated.\n";
  }

  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 => "v201710"});

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

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

Update a keyword

#!/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 updates the bid of a keyword. To get keywords, run
# basic_operations/get_keywords.pl.

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

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201710::AdGroupCriterionOperation;
use Google::Ads::AdWords::v201710::BiddableAdGroupCriterion;
use Google::Ads::AdWords::v201710::Criterion;

use Cwd qw(abs_path);

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

# Example main subroutine.
sub update_keyword {
  my $client      = shift;
  my $ad_group_id = shift;
  my $keyword_id  = shift;

  # Create base class criterion to avoid setting keyword specific fields.
  my $criterion =
    Google::Ads::AdWords::v201710::Criterion->new({id => $keyword_id,});

  # Create ad group criterion.
  my $ad_group_criterion =
    Google::Ads::AdWords::v201710::BiddableAdGroupCriterion->new({
      adGroupId => $ad_group_id,
      criterion => $criterion
    });

  # Create bids.
  my $bids = Google::Ads::AdWords::v201710::BiddingStrategyConfiguration->new({
      bids => [
        Google::Ads::AdWords::v201710::CpcBid->new({
            bid => Google::Ads::AdWords::v201710::Money->new(
              {microAmount => 1000000})}
        ),
      ]});

  $ad_group_criterion->set_biddingStrategyConfiguration($bids);

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

  # Update ad group criteria.
  my $result =
    $client->AdGroupCriterionService()->mutate({operations => [$operation]});

  # Display ad group criteria.
  if ($result->get_value()) {
    my $ad_group_criterion = $result->get_value()->[0];
    printf "Keyword with ad group id \"%d\", id \"%d\" was updated with bid " .
      "amount = \"%d\" micros.\n",
      $ad_group_criterion->get_adGroupId(),
      $ad_group_criterion->get_criterion()->get_id,
      $ad_group_criterion->get_biddingStrategyConfiguration()->get_bids()->[0]
      ->get_bid()->get_microAmount();
  } else {
    print "No keyword was updated.\n";
  }

  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 => "v201710"});

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

# Call the example
update_keyword($client, $ad_group_id, $keyword_id);

Send feedback about...

AdWords API
AdWords API
Need help? Visit our support page.