May 082012
 

(leer este artículo en español)

In this post we will explain how to use in Perl the v201109 version of the Google Adwords API. We make the assumption that the reader is already using a Google Adwords account, and that there are already in the account some adwords campaigns created, with some adgroups in them.

We start by downloading the library from Google’s website:
http://code.google.com/p/google-api-adwords-perl/downloads/list

When we access the above url in a browser, we find that the last available release of the library is in a file named awapi_perl_lib_2_5_6.tar.gz.

Once downloaded and uncompressed, we can try the sample perl script to retrieve the campaign identifiers:

#!/usr/bin/perl -w

use strict;
use warnings;
use lib "../adwords_api/awapi_perl_lib_2_5_6/lib";

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201109::OrderBy;
use Google::Ads::AdWords::v201109::Paging;
use Google::Ads::AdWords::v201109::Predicate;
use Google::Ads::AdWords::v201109::Selector;

use constant PAGE_SIZE => 500;
use Cwd qw(abs_path);
use POSIX;

sub get_campaigns {
 ...
}
# 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 => "v201109"
        });

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

# Call the example
get_campaigns($client);

In line three we write the location where we have put the library. In following lines, we include the library modules we are going to use.

Next comes the definition of the ‘get_campaigns()’ function, that we’ll review later, and finally the main program where we establish a connection to AdWords and call ‘get_campaigns()’.

sub get_campaigns {
  my $client = shift;

  # Create selector.
  my $paging = Google::Ads::AdWords::v201109::Paging->new({
    startIndex => 0,
    numberResults => PAGE_SIZE
  });
  my $selector = Google::Ads::AdWords::v201109::Selector->new({
    fields => ["Id", "Name"],
    ordering => [Google::Ads::AdWords::v201109::OrderBy->new({
      field => "Name",
      sortOrder => "ASCENDING"
    })],
    paging => $paging
  });
  # Paginate through results.
  my $page;
  do {
    # Get all campaigns.
    $page = $client->CampaignService()->get({serviceSelector => $selector});

    # Display campaigns.
    if ($page->get_entries()) {
      foreach my $campaign (@{$page->get_entries()}) {
        printf "Campaign with name "%s" and id "%d" was found.n",
               $campaign->get_name(), $campaign->get_id();
      }
    } else {
      print "No campaigns were found.n";
    }
    $paging->set_startIndex($paging->get_startIndex() + PAGE_SIZE);
  } while ($paging->get_startIndex() < $page->get_totalNumEntries());

  return 1;
}
 Posted by at 6:44 pm

  2 Responses to “How to work with the Google AdWords API in Perl”

  1. […] a previous post we explained how to develop perl script for the automated management of Google AdWords campaings, making use of a perl library that implements the API to access […]

 Leave a Reply

(required)

(required)