Section 14.4. Creating an AdGroup


14.4. Creating an AdGroup

With the Campaign ID (generated by Google), an AdGroup name, and the ad group's maximum CPC (both provided by a user), you can next create an AdGroup.

The AdGroup name is optional. If a name is not provided, Google will name it for you: AdGroup #1, and so on. The only required setting for an AdGroup is the maximum CPC, the maximum cost per click you are willing to pay (sometimes referred to in the AdWords API reference documentation as your bid).


14.4.1. Creating a Client Object and Setting Headers

In order to create an ad group, it's necessary to create a client web service object based on the AdGroupService web service. To do this, use the URL for the WSDL file to create a client object based on the AdGroupService web service:

     $adgroupwsdl = "https://adwords.google.com/api/adwords/v2/AdGroupService?wsdl";     $adgroupclient = new soapclient($adgroupwsdl, 'wsdl');

Since the authentication header has already been put together, this doesn't have to be done again. But the authentication header does have to be sent to the service each time a new client object is created. To do this, set the authentication header, using the $header variable that was previously constructed:

     $adgroupclient->setHeaders($header);

The $header variable can be accessed in the new module because PHP session tracking is engaged. The variable is used just like any other PHP variable.

If these two PHP programs were not running in the same session, though, you would need to re-create the headers manually.


14.4.2. Creating the AdGroup

The process of creating the ad group involves the following steps:

  1. Obtain values for AdGroup parameters you want to supply (the maximum CPC is required).

  2. Wrap the values in XML.

  3. Construct a string combining the values.

  4. Wrap the parameter string in XML.

  5. Use the XML-wrapped parameter string to add the AdGroup.

Here's the process put into practice. First, grab the AdGroup name and maxcpc using HTTP Post:

     $maxcpc = $_POST['maxcpc'];     $name = $_POST['name'];

Wrap these values in XML:

     $maxcpc = makeDocLit ("maxCpc", $maxcpc);     $name = makeDocLit ("name", $name);

The value for the maximum CPC is wrapped in XML using the tag maxCpc, for example, <maxCpc>50000</maxCpc>.


Construct the XML string for the new AdGroup using the values and the Campaign ID (accessed via session tracking):

     $adgroupParams = "<newdata> $maxcpc $campaignId $name</newdata>";     $campaignidparam = "<campaignID> $campaignId </campaignID>";     $adgroupParamsxml = "<addAdGroup> $campaignidparam $adgroupParams  </addAdGroup>";

Now add the AdGroup:

     $adgroup= $adgroupclient->call("addAdGroup", $adgroupParamsxml);     $adgroup = $adgroup['addAdGroupReturn'];

14.4.3. Getting the AdGroup ID

If there are any errors, they should be displayed. The program will stop processing, and a link will be provided for the user to start over again (all using the showErrors( ) method again).

Otherwise, an AdGroup ID has been created by Google. This internal identification for an AdGroup is what is needed to add creatives and keywords to the AdGroup. The AdGroup ID can be displayed and saved via session tracking for use in other modules (another module adds the creative and keyword to the AdGroup in this example):

     if($adgroupclient->fault) {        showErrors($adgroupclient);        echo '<a href="authenticate.php">Try again</a>';     }     else{        echo "<P>Ad Group   " . $adgroup['name'] .           " was created successfully.<br>\n<br>\n";        $adgroupid = "<adGroupId>" . $adgroup['id'] . "</adGroupId>";        session_register('adgroupid');     ...

If the AdGroup is successfully created, a form is dynamically generated for the user to enter an ad and the related keywords (as shown in Figure 14-4).

Figure 14-4. Once an AdGroup has been created, you can ad keywords and creatives to it


Example 14-4 shows the complete code for creating an AdGroup, as well as the form that is generated when the AdGroup is successfully created.

This form, as was the case with earlier examples, uses HTTP POST to open create_ad.php.


Example 14-4. Adding an AdGroup and getting input for an ad and keywords (adgroup.php)

 <?php session_start(  ) ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>AdGroup Creation</title> </head> <body> <?php require_once('NuSOAP/nusoap.php'); require_once('hd_lib.inc'); echo "<h1>AdGroup Creation</h1>";   // Connect to the WSDL file for the AdGroupService $adgroupwsdl = "https://adwords.google.com/api/adwords/v2/AdGroupService?wsdl"; $adgroupclient = new soapclient($adgroupwsdl, 'wsdl');   $adgroupclient->setHeaders($header);   $maxcpc = $_POST['maxcpc']; $name = $_POST['name'];   $maxcpc = makeDocLit ("maxCpc", $maxcpc); $name = makeDocLit ("name", $name);   // Add an AdGroup $adgroupParams = "<newdata> $maxcpc $campaignId $name</newdata>"; $campaignidparam = "<campaignID> $campaignId </campaignID>"; $adgroupParamsxml = "<addAdGroup> $campaignidparam $adgroupParams  </addAdGroup>";   $adgroup= $adgroupclient->call("addAdGroup", $adgroupParamsxml); $adgroup = $adgroup['addAdGroupReturn'];   // Handle any SOAP errors if($adgroupclient->fault) {    showErrors($adgroupclient);    echo '<a href="authenticate.php">Try again</a>'; } else{    echo "<P>Ad Group " . $adgroup['name'] .       " was created successfully.<br>\n<br>\n";    $adgroupid = "<adGroupId>" . $adgroup['id'] . "</adGroupId>";    session_register('adgroupid');    echo '<form action="create_ad.php" method="POST">    <table cellspacing=15>    <tr><td>    Enter ad Headline:    </td><td>    <input type="text" name="headline">    </td></tr><tr><td>    Enter first description line:    </td><td>    <input type="text" name="description1">    </td></tr><tr><td>    Enter second description line:    </td><td>    <input type="text" name="description2">    </td></tr><tr><td>    Enter display URL:    </td><td>    <input type="text" name="displayUrl">    </td></tr><tr><td>    Enter destination URL:    </td><td>    <input type="text" name="destinationUrl">    </td></tr><tr><td>    Enter keyword:    </td><td>    <input type="text" name="keyword1">    </td></tr><tr><td>    Enter keyword:    </td><td>    <input type="text" name="keyword2">    </td></tr><tr><td>    Enter keyword:    </td><td>    <input type="text" name="keyword3">    </td></tr><tr><td></td><td>    <input type="submit" name"Continue" value="Create Ad and Keywords">    </td></tr>    </table>    </form>'; } ?> </body> </html>



Google Advertising Tools. Cashing in with AdSense, AdWords, and the Google APIs
Google Advertising Tools: Cashing in with Adsense, Adwords, and the Google APIs
ISBN: 0596101082
EAN: 2147483647
Year: 2004
Pages: 145
Authors: Harold Davis

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net