How to retrieve Sitelinks urls.

481 views
Skip to first unread message

Emiliano Busiello

unread,
Jun 24, 2014, 4:18:18 AM6/24/14
to adwor...@googlegroups.com
I'm trying to get url out of sitelinks, I looked at this piece of code:

var campaigns = AdWordsApp.campaigns()
       
.withCondition("LabelNames CONTAINS_NONE['" + settings.sitelinksLabel +
           
"']")
       
.get();
   
while (campaigns.hasNext()) {
     
var campaign = campaigns.next();
     
var sitelinks = campaign.extensions().sitelinks().get();

     
Logger.log('Checking %s sitelinks.', sitelinks.totalNumEntities());
     
while (sitelinks.hasNext()) {
       
var sitelink = sitelinks.next();
       
var status = getUrlStatus(sitelink.getLinkUrl());
        accountSheet
.appendRow(['', sitelink.getLinkUrl(),
                                status
, campaign.getName(),
                               
'', '', '', sitelink.getLinkText()]);
     
}
      campaign
.applyLabel(settings.sitelinksLabel);
   
}

Taken from here, I also had a look around at the API documentation and at this example (which unfortunately doesn't treat get operations) and as far as I understand the FeedServiceInterface is what I should use. This is my code at the moment:

    val adWordsServices = new AdWordsServices()
    var offset = 0
    var limit = 100

    val service = adWordsServices.get(connection, classOf[FeedServiceInterface])

    val builder = new SelectorBuilder()
    var selector = builder
      .fields("Name", "Attributes")
      .offset(offset)
      .limit(limit)
.build() var page = service.get(selector) if (page.getEntries() != null) { var campaigns: List[Feed] = page.getEntries().toList campaigns.foreach(x => { val t: Array[FeedAttribute] = x.getAttributes t.foreach(x => { println(x.getId) println(x.getName) println(x.getType.getValue) }) }) }
First, this doesn't retun the urls (eg there's no getUrl or getValue or something similar), second, how do you know how to build your selector? More specifically, which fields I can query for? I searched for hours without finding anything, I got attributes right only by chance.

Anash P. Oommen (AdWords API Team)

unread,
Jul 11, 2014, 12:11:09 AM7/11/14
to adwor...@googlegroups.com
Hi,

You can use the getLinkUrl method of sitelink. See https://developers.google.com/adwords/scripts/docs/reference/adwordsapp/adwordsapp_sitelink for details. Scripts has a simplified version of FeedServiceInterface, so you need to rely mostly on the scripts documentation than the API documentation for this feature.

Cheers,
Anash P. Oommen,
AdWords Scripts Team.

Emiliano Busiello

unread,
Jul 11, 2014, 5:22:46 AM7/11/14
to adwor...@googlegroups.com
Hi Anash,

maybe I was not clear, how do you access the sitelink (and then consequently the getLinkUrl method)? Outside the google script environment I don't have access to an object called AdwordsApp, as I showed in my code I can only access the AdwordsServices class and then use one of the ServiceInterface interfaces, as showed also on many examples around, also from the adwords team on github. Using the FeedServiceInterface doesn't return what I actually want. Am I missing something obvious?

For completeness, this are the dependencies I imported using SBT:

      "com.google.http-client" % "google-http-client-jackson2" % "1.18.0-rc",
      "com.google.api-ads" % "ads-lib" % "1.27.0",
      "com.google.api-ads" % "adwords-axis" % "1.27.0"

Anash P. Oommen (AdWords API Team)

unread,
Jul 14, 2014, 2:47:05 PM7/14/14
to adwor...@googlegroups.com
Hi Emiliano,

If you plan on using AdWords API, then you need to ask on https://groups.google.com/forum/#!forum/adwords-api instead. The service you are looking for is FeedItemService and FeedMappingService. You need to retrieve the feeditem, and see if it has an attribute that is mapped to placeholder id = 1, placeholder field id = 2. See https://developers.google.com/adwords/api/docs/guides/feed-serviceshttps://developers.google.com/adwords/api/docs/appendix/placeholders for details.

Cheers
Anash P. Oommen,
AdWords Scripts Team.

Emiliano Busiello

unread,
Jul 17, 2014, 9:20:04 AM7/17/14
to adwor...@googlegroups.com
Hi Anash

Should I have asked somewhere else? Your link redirects exactly where I opened the post, anyway thanks for the answer, I'll try as soon as I can.

Emiliano Busiello

unread,
Jul 21, 2014, 4:18:39 AM7/21/14
to adwor...@googlegroups.com
Hi, I went through the documentation you linked, the example part on how to use the feed services is the same I linked in my initial post and there is no get operations specified, in fact all the operations called on services are mutate operations and that's not what I'm looking for, I don't want to populate a feed or build it up from scratch, I want to get feeds from the api.

Until now I managed to get the feeds using the feedServiceInterface and the placeholders, then you said to use the FeedMappingService, problem is the FeedMappingServiceInterface has only 3 operations, get( which takes a selector), get ( which takes a query string) and mutate (which takes operations).

At this point I don't know if I'm not explaining myself correctly or you are not reading my posts and just giving answers based on the title. I don't mean to be impolite, I really appreciate your time but please, try to give a complete answer instead of pieces that are confusing and misleading, if something is not clear on my part, please say so and I will do whatever I can to improve my question.

Josh Radcliff (AdWords API Team)

unread,
Jul 21, 2014, 1:59:17 PM7/21/14
to adwor...@googlegroups.com
Hi Emiliano,

If I'm understanding your previous posts, you are trying to use the Java client library to retrieve the URLs of sitelinks for a specific campaign.

In order to do this, you'll have to navigate through a few different types of objects. Note that when performing get (or query) requests, you can find the list of selectable fields with the proper names on our Selector Fields page.
  1. Use CampaignFeedService.get to fetch the CampaignFeed object for your campaign by filtering on CampaignId equal to your campaign ID, Status equal to ENABLED, and PlaceholderTypes equal to 1. (Note: for simplicity's sake, I'm ignoring the CampaignFeed's matchingFunction, which may further limit the FeedItems by feedItemId).
  2. Use FeedMappingService.get to fetch the FeedMappings for PlaceholderType 1. Look through the returned FeedMappings and their attributeFieldMappings to determine which attribute of your Feed is mapped to field placeholder 2 (for URL).
  3. Use FeedItemService.get to retrieve all FeedItems for the feedId found in step 1. On each FeedItem returned, Inspect the attribute identified in step 2 to get the URL field value.
Below is a complete code sample I put together in Java. Please let me know if this does not cover your questions.

Cheers,
Josh, AdWords API Team

import com.google.api.ads.adwords.axis.factory.AdWordsServices;
import com.google.api.ads.adwords.axis.utils.v201406.SelectorBuilder;
import com.google.api.ads.adwords.axis.v201406.cm.AttributeFieldMapping;
import com.google.api.ads.adwords.axis.v201406.cm.CampaignFeed;
import com.google.api.ads.adwords.axis.v201406.cm.CampaignFeedServiceInterface;
import com.google.api.ads.adwords.axis.v201406.cm.FeedItem;
import com.google.api.ads.adwords.axis.v201406.cm.FeedItemAttributeValue;
import com.google.api.ads.adwords.axis.v201406.cm.FeedItemServiceInterface;
import com.google.api.ads.adwords.axis.v201406.cm.FeedMapping;
import com.google.api.ads.adwords.axis.v201406.cm.FeedMappingServiceInterface;
import com.google.api.ads.adwords.axis.v201406.cm.Selector;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.common.lib.auth.OfflineCredentials;
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
import com.google.api.client.auth.oauth2.Credential;

public class GetCampaignSiteLinks {

  public static void main(String[] args) throws Exception {
    Credential oAuth2Credential = new OfflineCredentials.Builder().forApi(Api.ADWORDS).fromFile()
        .build().generateCredential();

    // Construct an AdWordsSession.
    AdWordsSession session =
        new AdWordsSession.Builder().fromFile().withOAuth2Credential(oAuth2Credential).build();

    AdWordsServices adWordsServices = new AdWordsServices();

    Long campaignId = Long.valueOf("INSERT_CAMPAIGN_ID_HERE");
    
    getCampaignSitelinkUrls(campaignId, session, adWordsServices);
  }
  
  private static void getCampaignSitelinkUrls(Long campaignId, AdWordsSession session,
      AdWordsServices adWordsServices) throws Exception {
    // Find the ENABLED CampaignFeed that's mapped to the Campaign for sitelinks (placeholder type
    // of 1).
    CampaignFeedServiceInterface campaignFeedService = adWordsServices.get(session,
        CampaignFeedServiceInterface.class);
    Selector selector = new SelectorBuilder()
        .fields("CampaignId", "FeedId")
        .equals("PlaceholderTypes", "1")
        .equals("CampaignId", campaignId.toString())
        .equals("Status", "ENABLED")
        .build();
    CampaignFeed campaignFeed = campaignFeedService.get(selector).getEntries(0);
    
    // Find the ENABLED FeedMapping for the feed above.
    FeedMappingServiceInterface feedMappingService = adWordsServices.get(session,
        FeedMappingServiceInterface.class);
    selector = new SelectorBuilder()
        .fields("FeedId", "AttributeFieldMappings")
        .equals("PlaceholderType", "1")
        .equals("Status", "ENABLED")
        .equals("FeedId", campaignFeed.getFeedId().toString())
        .build();
    FeedMapping feedMapping = feedMappingService.get(selector).getEntries(0);
    
    // Find the attribute that's mapped to the URL placeholder field (2).
    AttributeFieldMapping urlAttributeFieldMapping = null;
    for(AttributeFieldMapping attributeFieldMapping : feedMapping.getAttributeFieldMappings()) {
      if(attributeFieldMapping.getFieldId().intValue() == 2) {
        urlAttributeFieldMapping = attributeFieldMapping;
        break;
      }
    }
    
    // Find the FeedItems on the feed and print each one's ID and URL
    FeedItemServiceInterface feedItemService =
        adWordsServices.get(session, FeedItemServiceInterface.class);
    selector = new SelectorBuilder()
        .fields("FeedId", "FeedItemId", "AttributeValues")
        .equals("FeedId", campaignFeed.getFeedId().toString())
        .build();
    
    for(FeedItem feedItem : feedItemService.get(selector).getEntries()) {
      // Get the FeedItemAttributeValue for the feed attribute ID identified above from
      // the FeedMapping.
      FeedItemAttributeValue urlAttributeValue = null;
      for(FeedItemAttributeValue attributeValue : feedItem.getAttributeValues()) {
        if (attributeValue.getFeedAttributeId().equals(
            urlAttributeFieldMapping.getFeedAttributeId())) {
          urlAttributeValue = attributeValue;
          break;
        }
      }
      System.out.printf("Feed item ID %d has URL %s.%n", feedItem.getFeedItemId(),
          urlAttributeValue.getStringValue());
    }
  }
}

Emiliano Busiello

unread,
Jul 22, 2014, 9:02:21 AM7/22/14
to adwor...@googlegroups.com
Hi Josh, thanks for the documentation link to the queryable fields and the code example, this is exactly what I was looking for.
Reply all
Reply to author
Forward
0 new messages