Section 15.3. Iterating Through the AdWords Hierarchy

15.3. Iterating Through the AdWords Hierarchy

With each proxy class for the various AdWords API web services instantiated and authenticated, it is time to use these classes to interact with AdWords information.

It's good practice in a real application to place code within TRy...catch...finally blocks (which I've omitted here in the interests of simplicity). In particular, calls to web services should always be placed using the TRy syntax, because so many things can go wrong with remote calls. In addition, good code includes exception handling (once again omitted here in the interests of simplicity).

It's worth bearing in mind that an actual application would probably do more than simply iterating through the AdWords objects. Your code could compare values retrieved from the AdWords objects to find specific items and then update the values associated with these items using criteria generated by your internal systems.

15.3.1. Displaying Campaign Information

With the preliminaries accomplished, the getAllAdWordsCampaigns( ) method of the CampaignService web service can be called:

     Campaign[] campaignList = service.getAllAdWordsCampaigns(42);

The method assigns the information about all the campaigns in an account into an array of Campaign objects named campaignList. (The Campaign class is defined in the web service proxy class.)

The getAllAdWordsCampaigns( ) method is passed an integer argument. However, this argument is referred to by the Google documentation as "dummy" informationit is never used and can be any positive integer.

Next, for every Campaign in the campaignList array, add the internal identification for the related campaign to a node in a TreeView control:

     foreach (Campaign c in campaignList)     {        TreeNode c_node = treeView1.Nodes.Add("Campaign " +   ) + ": " +;        ...     }

15.3.2. Displaying AdGroup Information

For each campaign, you can display the AdGroups that are part of the campaign using the ID for the specific campaign.

Within the foreach loop that adds each campaign to a node in the TreeView control, you can add subnodes for each AdGroup using the campaign ID:

     //add a campaign node     ...     AdGroup[] agList = ag.getAllAdGroups(;     foreach(AdGroup a in agList)       // now add the AdGroup info nodes

It turns out, however, that this code fails. It works fine on campaigns that contain one or more AdGroups, but returns an object reference error when it encounters a campaign without AdGroups.

Deleted campaigns hang around. Even once you've deleted a campaign in AdWords, it will still be returned by the APIs (with a Deleted status).

Most AdWords users will have campaigns without AdGroups, particularly if you consider deleted campaigns. So it's necessary first to test for the existence of AdGroups (before one can attempt to display them):

     if (ag.getAllAdGroups( != null)

Here's the code snippet that checks to make sure that there is at least one AdGroup for a specific campaign ID. It then cycles through all the AdGroups in the campaign, adding the ID and a human-readable name to a subnode on the TreeView:

     if (ag.getAllAdGroups( != null)     {        AdGroup[] agList = ag.getAllAdGroups(;        foreach(AdGroup a in agList)        {           TreeNode ag_node = c_node.Nodes.Add("AdGroup "              +  ) + ": " +;           ...

15.3.3. Showing Keywords Associated with an Ad

Using the ID returned for an AdGroup, the same technique works to access keywords associated with the AdGroup.

You could also use the AdGroup ID to retrieve the creative (and its elements) associated with an AdGroup.

The first step is to check to see that there is at least one keyword to retrieve:

     if (ks.getAllKeywords( != null)

Next, cycle through the Keywords array to retrieve the text of individual keywords and display them as third-level nodes:

     if (ks.getAllKeywords( != null)       foreach (KeywordService.Keyword k in keywordList)       {         ag_node.Nodes.Add(k.text);       }

15.3.4. Displaying Results

The Windows application shown in Figure 15-8 can now be used to display the AdWords objects as nodes, with three levels of depth: campaigns, ads, and keywords.

Figure 15-8. It's useful to be able see the keywords you have in place across campaigns and ad groups

The user enters authentication information for the AdWords account and clicks Display. The initial display will show campaign IDs and names. Expanding the campaign node, by clicking the plus icon (+) to the left of the node, will show the AdGroups it contains, and expanding an AdGroup node will show the keywords associated with that ad.

Example 15-1 shows the complete code from a Windows form project that displays AdWords API nodes using a TreeView control.

Example 15-1. Displaying account information using a TreeView control; navigating the hierarchy of information provided by the various Google ad web services allows you to drill down to specific ads you want to manipulate

 using System; ... using AccountInfo.CampaignService; using AccountInfo.AdGroupService; using AccountInfo.KeywordService; namespace AccountInfo {    public class Form1 : System.Windows.Forms.Form    {      private System.Windows.Forms.TreeView treeView1;      private System.Windows.Forms.GroupBox groupBox1;      private System.Windows.Forms.TextBox txtEmail;      private System.Windows.Forms.TextBox txtPassword;      private System.Windows.Forms.TextBox txtUserAgent;      private System.Windows.Forms.TextBox txtSecret;      ...      private System.Windows.Forms.Button btnDisplay;      CampaignServiceService service = new CampaignServiceService(  );      AdGroupServiceService ag = new AdGroupServiceService(  );      KeywordServiceService ks = new KeywordServiceService(  );      ...      public Form1(  )      {        ...        static void Main(  )        {            Application.Run(new Form1(  ));        }        private void btnDisplay_Click(object sender, System.EventArgs e)        {            service.emailValue = new  );            service.emailValue.Text = new String[] {txtEmail.Text};            service.passwordValue = new CampaignService.password(  );            service.passwordValue.Text = new String[] {txtPassword.Text};            service.useragentValue = new CampaignService.useragent(  );            service.useragentValue.Text = new String[] {txtUserAgent.Text};            service.tokenValue = new CampaignService.token(  );            service.tokenValue.Text = new String[] {txtSecret.Text};            ag.emailValue = new  );            ag.emailValue.Text = new String[] {txtEmail.Text};            ag.passwordValue = new AdGroupService.password(  );            ag.passwordValue.Text = new String[] {txtPassword.Text};            ag.useragentValue = new AdGroupService.useragent(  );            ag.useragentValue.Text = new String[] {txtUserAgent.Text};            ag.tokenValue = new AdGroupService.token(  );            ag.tokenValue.Text = new String[] {txtSecret.Text};            ks.emailValue = new  );            ks.emailValue.Text = new String[] {txtEmail.Text};            ks.passwordValue = new KeywordService.password(  );            ks.passwordValue.Text = new String[] {txtPassword.Text};            ks.useragentValue = new KeywordService.useragent(  );            ks.useragentValue.Text = new String[] {txtUserAgent.Text};            ks.tokenValue = new KeywordService.token(  );            ks.tokenValue.Text = new String[] {txtSecret.Text};            treeView1.Visible=false;            // retrieve all campaigns and display the data            Campaign[] campaignList = service.getAllAdWordsCampaigns(42);            foreach (Campaign c in campaignList)            {               // Display a wait cursor while the TreeNodes are being created.               Cursor.Current = Cursors.WaitCursor;               TreeNode c_node = treeView1.Nodes.Add("Campaign " +       ) + ": " +;               if (ag.getAllAdGroups( != null)               {                  AdGroup[] agList = ag.getAllAdGroups(;                  foreach(AdGroup a in agList)                  {                     TreeNode ag_node = c_node.Nodes.Add("AdGroup "                        +  ) + ": " +;                     if (ks.getAllKeywords( != null)                     {                        KeywordService.Keyword[] keywordList =                           ks.getAllKeywords(;                        foreach (KeywordService.Keyword k in keywordList)                        {                           ag_node.Nodes.Add(k.text);                        }                     }                  }               }               // Reset the cursor to the default.               Cursor.Current = Cursors.Default;            }            treeView1.Visible=true;        }    } }

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

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: