15.3. Iterating Through the AdWords HierarchyWith 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 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 InformationWith 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.)
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 " + c.id.ToString( ) + ": " + c.name); ... } 15.3.2. Displaying AdGroup InformationFor 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(c.id); 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.
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(c.id) != 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(c.id) != null) { AdGroup[] agList = ag.getAllAdGroups(c.id); foreach(AdGroup a in agList) { TreeNode ag_node = c_node.Nodes.Add("AdGroup " + a.id.ToString( ) + ": " + a.name); ... 15.3.3. Showing Keywords Associated with an AdUsing the ID returned for an AdGroup, the same technique works to access keywords associated with the AdGroup.
The first step is to check to see that there is at least one keyword to retrieve: if (ks.getAllKeywords(a.id) != null) Next, cycle through the Keywords array to retrieve the text of individual keywords and display them as third-level nodes: if (ks.getAllKeywords(a.id) != null) foreach (KeywordService.Keyword k in keywordList) { ag_node.Nodes.Add(k.text); } 15.3.4. Displaying ResultsThe 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 groupsThe 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
|