API: Making Bulk Changes

API: Making Bulk Changes

The Problem

Your advertiser has just e-mailed to inform you that they have acquired a shorter domain name. They would like you to change all of their ads to redirect to the new, shorter domain name. So, you log in to AdvertPro, go the media listing, filter it by their advertiser account and as you were fearing they have around a hundred Flash banners in total. Editing each of those media to change the redirect is not only going to be time consuming, it's going to be error-prone because you could easily miss one of them in the process.

API to the Rescue!

Why not use the API to make all of the changes for you? It can take care of them in a matter of seconds. You just have to create a quick script that does the following:

  1. Query the API for a list of Flash banner media owned by the advertiser
  2. Do a search/replace for the domain name on the clickTAG of each Flash banner media
  3. Use the API to update the media with the new clickTAG value

The Solution

Here we have a Perl script that will do a search/replace to change www.xyzcorporation.com to www.xyz.com in the ClickTAG URL for all Flash banners owned by the advertiser.

#!/usr/bin/perl -w

#
# Load Required Modules
#

use strict;
use LWP::UserAgent;
use XML::Simple;

#
# API Configuration -- Edit This Part
#

my $API_BASE_URL = "http://www.example.com";
my $API_SECRET_KEY = "5eb63bbbe01eeed093cb22bb8f5acdc3";

#
# Advertiser Information -- Edit This Part
#

my $ADVERTISER_ID = 1;
my $OLD_DOMAIN = "www.xyzcorporation.com";
my $NEW_DOMAIN = "www.xyz.com";
my $FLASH_TYPE_ID = 11;

#
# Here we set up an HTTP POST requests to query for the list
# of Flash banner media owned by the advertiser.
#
# The media type for Flash banners is 11, which you will find
# in the media API documentation.
#

my %query_params = (
  "secret" => "$API_SECRET_KEY",
  "advertiser" => $ADVERTISER_ID,
  "type" => $FLASH_TYPE_ID
);

my $query_ua = LWP::UserAgent->new;

my $query_response = $query_ua->post(
  "$API_BASE_URL/servlet/control/api/media/query", \%query_params
);

#
# If the API request was successful, parse the returned XML
# and loop through each media.
#

if ($query_response->is_success)
{
  my $xml = XMLin($query_response->decoded_content, KeyAttr => []);
  
  if ($xml && $xml->{'media'})
  {
    foreach my $media ( @{ $xml->{'media'} } )
    {
      #
      # Test to see if the clickTAG contains a reference to the
      # old domain name. If it does we can replace it with the
      # new domain name.  Then we can make another API request
      # to update the media on the ad server with the change.
      #
      
      my $clicktag = $media->{'creative'}->{'flash'}->{'clicktag'};
      
      if ($clicktag =~ /$OLD_DOMAIN/i)
      {
        $clicktag =~ s/$OLD_DOMAIN/$NEW_DOMAIN/ig;
        
        my $update_ua = LWP::UserAgent->new;
        
        my %update_params = (
          "secret" => "$API_SECRET_KEY",
          "id" => $media->{'id'},
          "flash_clicktag" => $clicktag
        );
        
        my $update_response = $update_ua->post(
          "$API_BASE_URL/servlet/control/api/media/update", \%update_params
        );

        if ($update_response->is_success)
        {
          print "Updated successfully: " . $media->{'name'} .
                " (ID# " . $media->{'id'} . ")\n";
        }

        else
        {
          die $update_response->status_line;
        }
      }
    }
  }
}

else
{
  die $query_response->status_line;
}


Some changes are necessary at the beginning of the script to change your API base URL and your secret API key as well as the advertiser ID# and old/new domain names.

More Solutions

This is just a simple example of how the API can save you time by performing a repetitive task for you. To learn more, continue reading the API chapter in the user manual: http://www.advertpro.com/docs/2.0/html/manual/api.html Remember, the API can work with any programming language! I personally just like Perl. Feel free to use Java, PHP, Ruby or whatever you're most comfortable coding in.

← API: Security Best Practices
Contextual Targeting →