Printer Friendly Version      Send     
Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Windows Live
 How to Request and Download a Keywo...
Windows Live
How to Request and Download a Keyword Performance Report in Perl (V5.1)

The following code sample shows how to request and retrieve a keyword performance report by using Perl.

# For release code, use the following SOAP::Lite statement.
use SOAP::Lite ( +maptype => {} );
# For debugging code, use the following SOAP::Lite statement.
#use SOAP::Lite ( +trace => all, maptype => {} );

use LWP::Simple;
use File::Basename;
use strict;

eval
{
    my $username;
    my $password;
    my $devtoken;
    my $accountID;
    my $zipFile;
    
    # Get the parameters from the program arguments.
    unless (@ARGV == 5)
    {
        print "Usage: ";
        print "file.pl username password devtoken accountID reportfile.zip\n";
        die;
    }
    $username = $ARGV[0];
    $password = $ARGV[1];
    $devtoken = $ARGV[2];
    $accountID = $ARGV[3];
    $zipFile = $ARGV[4];
    
    # Use either the sandbox or production URI.
    # This example is for the sandbox URI.
    my $URI = "https://sandboxapi.adcenter.microsoft.com/api/advertiser/v5.1";
    # The following commented-out line contains the production URI.
    #my $URI = https://adcenterapi.microsoft.com/api/advertiser/v5.1";
    
    # The adCenter API namespace.
    my $xmlns = "https://adcenter.microsoft.com/api/advertiser/v5";
    
    # The proxy for the reporting Web service.
    my $reportingProxy = $URI."/Reporting/ReportingService.svc?wsdl";
    
    # The common data-type namespace.
    my $namespaceArrays =
        "http://schemas.microsoft.com/2003/10/Serialization/Arrays";
    
    # The service operation that will be called.
    my $action = 'QueueReport';
    
    # The Web service.
    my $reportingService = SOAP::Lite->new(
        uri => $URI, 
        proxy => $reportingProxy, 
        on_action => ( sub { return $action } ));
    $reportingService->autotype(0);
    $reportingService->multirefinplace(1);
    $reportingService->readable(1);
    
    my $ReportRequest = CreateKeywordPerformanceReportRequest(
        'My Keyword Report',
        $accountID);
    
    my $reportRequestId;
    
    # Assemble the service operation parameters.
    my @header = CreateV5Header($username, $password, $devtoken);
    my @params =
    (
        @header,
        $ReportRequest,
        $reportRequestId
    );
    
    # Make the adCenter API call.
    my $methodQueueReport = SOAP::Data->name
    (
        $action."Request"
    )->attr({xmlns => $xmlns});
    
    my $response = $reportingService->call($methodQueueReport => @params);
    
    # Check for errors.
    if ($response->fault) 
    {
        print "$action failed.\n";
        
        # Display the fault code and the fault string.
        print $response->faultcode, " ", $response->faultstring, "\n";
    
        # Display the adCenter tracking ID.
        my $trackingId = $response->valueof("//ApiFaultDetail/TrackingId");
        print "TrackingId: $trackingId\n";
        
        # Display the adCenter operation errors.
        my @operationErrors = $response->valueof(
            "//ApiFaultDetail/OperationErrors/OperationError");
        foreach my $operationError (@operationErrors)
        {
            print "Operation error ($operationError->{Code}) encountered. ";
            print "$operationError->{Message}\n";        
        }
    }
    else
    {
        print "Successful call to $action.\n";
    
        # Display the adCenter tracking ID.
        my $trackingId = $response->valueof(
            '//ApiCallTrackingData/TrackingId');
        print "TrackingId: $trackingId\n";
        
        my $reportId = $response->valueof('//ReportRequestId');
        $reportRequestId = $reportId;
        
        RetrieveReport($reportId, $zipFile);
    }
    
    sub CreateV5Header
    {
        # Method parameters.
        my ($username, $password, $devtoken) = @_;
        
        # Initialize the application token.
        my $AppToken = SOAP::Header->name
        (
            "ApplicationToken"=>{Value => ""}
        )->attr({xmlns => $xmlns});
        
        # Initialize the developer token.
        my $DevToken =SOAP::Header->name
        (
            "DeveloperToken"=>{Value=>$devtoken}
        )->attr({xmlns => $xmlns});
        
        # Initialize the user credentials.
        my $UserCreds = SOAP::Header->name("UserCredentials" => 
        {
            Username=>SOAP::Data->name("Username" => $username),
            Password=>SOAP::Data->name("Password" => $password)
        })-> attr({xmlns => $xmlns});
    
        # Assemble the header parameters.
        my @headerParams =
        (
            $DevToken, 
            $AppToken,
            $UserCreds, 
        );
        
        return @headerParams;
    }
    
    # This subroutine creates the keyword performance report request. Some of
    # the data is passed in to the subroutine, but the majority of it is
    # hard-coded.
    sub CreateKeywordPerformanceReportRequest
    {
        my ($nameOfReport, $accountForReport) = @_;
        
        # Use the English language.
        my $language = SOAP::Data->name("Language" => "English");
        
        # Request an XML report.
        my $format = SOAP::Data->name("Format" => "Xml");
    
        # Allow partial data to be returned.
        my $returnOnlyCompleteData = SOAP::Data->name(
            "ReturnOnlyCompleteData" => "false");
        
        # Specify the report name.
        my $reportName = SOAP::Data->name("ReportName" => $nameOfReport);
        
        # Specify the report aggregation.
        my $aggregation = SOAP::Data->name("Aggregation" => "Monthly");
        
        # Specify the time frame of the report. For this example, it is the
        # last six months.
        my $predefinedTime = SOAP::Data->name(
            "PredefinedTime" => "LastSixMonths");
        my $time = SOAP::Data->name("Time" => [$predefinedTime]);
        
        # Specify the account that the report is for.
        my $accountIds = SOAP::Data->name("AccountIds" =>
        [
            SOAP::Data->name("int")->value(
                $accountForReport)->uri($namespaceArrays)
        ]);
        my $scope = SOAP::Data->name("Scope" => [$accountIds]);
    
        # Specify the columns that will be in the report.
        my $columns = SOAP::Data->name("Columns" =>
        [
            SOAP::Data->name("KeywordPerformanceReportColumn" =>
                "AccountName"),
            SOAP::Data->name("KeywordPerformanceReportColumn" =>
                "CampaignName"),
            SOAP::Data->name("KeywordPerformanceReportColumn" =>
                "Keyword"),
            SOAP::Data->name("KeywordPerformanceReportColumn" =>
                "TimePeriod"),
            SOAP::Data->name("KeywordPerformanceReportColumn" =>
                "Impressions"),
            SOAP::Data->name("KeywordPerformanceReportColumn" =>
                "Conversions"),
        ]);
        
        # Specify the filter for the report. The report request 
        # in this example specifies only the search ads that were
        # displayed in the United States.
        my $adDistribution = SOAP::Data->name("AdDistribution" => "Search");
        my $languageAndRegion = SOAP::Data->name(
            "LanguageAndRegion" => "UnitedStates");
        my $filter = SOAP::Data->name("Filter" =>
        [
            $adDistribution,
            $languageAndRegion
        ]);
        
        # Create the report request object.
        my $request = SOAP::Data->name("ReportRequest" =>
        [
            $format,
            $language,
            $reportName,
            $returnOnlyCompleteData,
            $aggregation,
            $columns,
            $filter,
            $scope,
            $time
        ]);
    
        # Set the request attributes.
        $request->attr
        ({
            'i:type' => 'KeywordPerformanceReportRequest',
            'xmlns:i' => 'http://www.w3.org/2001/XMLSchema-instance'
        });
        
        return $request;
    }
    
    
    # This subroutine calls the GetReportStatus service operation.
    sub GetReportStatus
    {
        my $reportRequestId=$_[0];
        
        my @header = CreateV5Header($username, $password, $devtoken);
        
        # This is important because the on_action property of the Web
        # service uses a reference to this global variable. If you don't
        # change the global variable, the wrong service operation might be
        # called.
        $action = "GetReportStatus";
        
        my @params =
        (
            @header,
            SOAP::Data->name("ReportRequestId" => $reportRequestId)
        );
        
        my $methodGetReportStatus = SOAP::Data->name(
            $action.'Request')->attr({xmlns => $xmlns});
        my $response = $reportingService->call(
            $methodGetReportStatus => @params);
    
        if ($response->fault) 
        {
            print "$action failed.\n";
            
            # Display the fault code and the fault string.
            print $response->faultcode, " ", $response->faultstring, "\n";
        
            # Display the adCenter tracking ID.
            my $trackingId =
                $response->valueof("//ApiFaultDetail/TrackingId");
            print "TrackingId: $trackingId\n";
            
            # Display the adCenter operation errors.
            my @operationErrors = $response->valueof(
                "//ApiFaultDetail/OperationErrors/OperationError");
            foreach my $operationError (@operationErrors)
            {
                print
                    "Operation error ($operationError->{Code}) encountered. ";
                print "$operationError->{Message}\n";        
            }
        }
        else
        {
            print "Successful call to $action.\n";
        }
    
        return $response;
    }
    
    # This subroutine retrieves a requested report. This subroutine will work
    # for any type of report. The only parameter needed for the subroutine is
    # the report request identifier.
    sub RetrieveReport
    {
        my ($reportRequestId, $zipFileName) = @_;
        my $polling = 1;
        my $waitMinutes = 15;
        my $content = "";
    
        while ($polling)
        {
            my $response = GetReportStatus($reportRequestId);
            my $status = $response->valueof('//Status');
            print "Status: $status \n\r";
            
            if ($status =~ /Success/)
            {
                # Get the URL of the report to download.
                my $downloadUrl = $response->valueof('//ReportDownloadUrl');
                print "Downloading From : $downloadUrl\n\r";
    
                # Download the contents of the zipped report.
                unless (defined ($content = get($downloadUrl)))
                {
                    die;
                }
    
                # Ensure that the ZIP file destination directory exists.
                my $dirname = dirname($zipFileName);
                mkdir($dirname);
                
                # Open the ZIP file for writing.
                open(ZIPFILE, ">", $zipFileName);
                
                # Set the ZIP file to binary mode.
                binmode(ZIPFILE);
                
                # Write the contents of the report to the ZIP file.
                print(ZIPFILE $content);
                
                # Close the ZIP file.
                close(ZIPFILE);
                
                $polling = 0;
            }
            
            if (!($status =~ /Pending/))
            {
                $polling = 0;
            }
    
            if($polling)
            {
                # Wait a while before getting the status again.
                sleep($waitMinutes * 60);
            }
        }
    
        return $content;
    }
    
};
warn $@ if $@;
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker