This article explains how to create sitelinks using the AdWords API Feed Services: a generic tool for creating ad extensions by creating feeds and associating them with your campaigns. The Feed Services have operations for most of the sitelink settings available in AdWords, but some of them are currently only available to beta testers:
modifying bids at the ad group level,
choosing sitelink descriptions, and
selecting the dates and times that the sitelinks will run.
It's hard to get access to the beta program, so you can't use these features for now and I won't discuss them further.
Creating Sitelinks Using the Feed Services
A first glance of the Feed Service example code appears quite complicated, but, once you get the overall picture, using it is not so difficult. The idea is to create a generic list of attributes, associate each attribute to a sitelink field, and then associate the feed with a campaign. The process has four steps:
Create a new feed with fields for the sitelink URL and text.
Populate the feed with your sitelinks.
Associate the feed with the sitelinks extension.
Link the feed to your campaigns.
In the next four sections of the tutorial, I will explain each step using the following example feed:
I'm going to store these in a simple array for use in the rest of the script:
This code represents each sitelink using an associative array containing an id, text, and URL. The ids are currently empty as the AdWords API will generate them when the script uploads each item.
This script creates a feed, called Site Links Feed, with an empty set of attributes and a USER origin (the origin just specifies who created the feed, which will always be the user). Now we can add the two sitelink fields to the feed:
This code creates two FeedAttribute objects: one string field called Link Text and one URL field called Link URL. You can now send the Feed to the API using a mutate call:
The results should contain a new Feed object with ids that you need to extract for use in the next steps:
Populating the Feed with Sitelinks
Now that we have a feed, we can add some sitelinks to it using the FeedItemService. This service takes a FeedItem object for each of your sitelinks, and each FeedItem has two FeedItemAttributeValue objects: one containing the sitelink text and another containing the URL. Here's some code that creates a feed item with the two fields:
This code creates a FeedItem using the $feedId from the previous step, then sets the link text and link URL fields using two FeedItemAttributeValue objects. The code selects the appropriate fields using the $linkTextId and $linkUrlId and sets the values using the stringValue property. You can upload the item to the API using a mutate operation:
Given this, you can create each of the example sitelinks using the following code:
This script creates a new feed item for each sitelink in the array, and then uploads them to the AdWords API. You need to extract the id of each sitelink from the result:
You will use these ids later when you import the sitelinks into your campaigns.
Associating the Feed with the Sitelinks Extension
The feed that we just created is currently only a generic list of attributes, not a set of sitelinks; to turn it into sitelinks, you need to tell the API that the feed contains sitelinks using the FeedMappingService. This service takes a FeedMapping object that maps your feed onto an ad extension. You select the extension type using an enumeration of placeholder types, then link each feed attribute to an extension field using an AttributeFieldMapping object.
The sitelinks placeholder type is 1, so you can create a FeedMapping using the following code:
Next you need to map the text feed attribute to the sitelink field and the URL feed attribute to the sitelink URL field:
Now you can upload the FeedMapping to the API:
You should now find the feed in the Ad extensions tab of the AdWords UI:
Importing the Sitelinks into a Campaign
Now that you've got a list of sitelinks, you can link them to a campaign using the CampaignFeedService. This service takes a CampaignFeed object with your campaign and feeds ids and the type of extension contained in the feed:
In addition to this, you also need to select which sitelinks to include in the campaign using a FeedFunction. A FeedFunction is a generic function that you can use to match items within a feed. To select an individual sitelink, you construct a function that selects feed items matching a specific feed item id:
This code creates a function that matches feed items with a FEED_ITEM_ID field that EQUALS the chosen $feedItemId. To include multiple sitelinks, use the IN operator to specify a set of ids:
Here's a full example of attaching the sitelinks to a campaign (Note that if you already have some sitelinks attached to the campaign, then you will need to remove them before running this script, otherwise you'll get a CampaignFeedError.FEED_ALREADY_EXISTS_FOR_PLACEHOLDER_TYPE error):
Your campaign should now have the new sitelinks selected:
Full Example Script
Here's a complete sample script for creating sitelinks using the AdWords API: