GOOGLE-ADWORDS-PERL-CLIENT

 view release on metacpan or  search on metacpan

examples/v201309/basic_operations/get_ad_groups.pl  view on Meta::CPAN

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::v201309::Predicate->new({
    field => "CampaignId",
    operator => "IN",
    values => [$campaign_id]
  });

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

  # Paginate through results.
  my $page;
  do {
    # Get ad groups.
    $page = $client->AdGroupService()->get({serviceSelector => $selector});

    # Display ad groups.
    if ($page->get_entries()) {
      my @results = ref($page->get_entries()) eq "ARRAY" ?
          @{$page->get_entries()} : ($page->get_entries());
      foreach my $ad_group (@results) {
        printf "Ad group with name \"%s\" and id \"%d\" was found.\n",
               $ad_group->get_name(), $ad_group->get_id();
      }
    } else {
      print "No ad groups were found.\n";
    }
    $paging->set_startIndex($paging->get_startIndex() + PAGE_SIZE);
  } while ($paging->get_startIndex() < $page->get_totalNumEntries());

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

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



( run in 0.908 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )