how to Get SiteLink Final Urls in Java

58 views
Skip to first unread message

Karim Benna

unread,
Mar 15, 2016, 7:45:31 AM3/15/16
to AdWords API Forum
Hallo, 

I'm sorry maybe this answer has already an answer on PhP but i couldn't translate is into Java AdWords API. Any way i'm trying to get all the finalUrls of SiteLinks if there is some.

As i understand,i have to user the AdGroupExtensionSettingService to get the siteLinks and then initialize a variable from Type SiteLinkFeed and get the finalUrls. But the problem is that i could initialize a variable of Type SiteLinkFeed 
from ExtensionFeedItem Type.

This is the code i've written and i don't know if i'm in the wrong or right way. Thank you very much for helping me.



AdGroupExtensionSettingServiceInterface adGroupExtentionService = adWordsServices.get(session,
 
AdGroupExtensionSettingServiceInterface.class);

int offset4 = 0;
SelectorBuilder builder4 = new SelectorBuilder();
Selector selector4 = builder4
.fields(AdGroupExtensionSettingField.Extensions, AdGroupExtensionSettingField.AdGroupId)
.in(AdGroupExtensionSettingField.AdGroupId, splittedAdGroupIds.get(k))
.equals(AdGroupExtensionSettingField.ExtensionType, "SITELINK")
.orderAscBy(AdGroupExtensionSettingField.AdGroupId).offset(offset4).limit(PAGE_SIZE).build();

AdGroupExtensionSettingPage page4 = null;
do {
// Get all campaigns.
page4 = adGroupExtentionService.get(selector4);
if (page4.getEntries() != null) {
for (AdGroupExtensionSetting adGroupExtenstion : page4.getEntries()) {
try {
ExtensionFeedItem[] feedItemArray = adGroupExtenstion.getExtensionSetting().getExtensions();
for(ExtensionFeedItem feedItem : feedItemArray){
if(feedItem.getFeedType().getValue() == "SITELINK"){
System.out.println("Feed Item Found : " + feedItem);
}
}
} catch (NullPointerException e) {
System.out.println("No final urls from Extensions found");
}
}
} else {
System.out.println("No adGroupExtensionSetting were found.");
}
offset4 += PAGE_SIZE;
selector4 = builder4.increaseOffsetBy(PAGE_SIZE).build();
} while (offset4 < page4.getTotalNumEntries());





Anthony Madrigal

unread,
Mar 15, 2016, 4:46:19 PM3/15/16
to AdWords API Forum
Hi Karim,

In order to get the final URLs of your sitelinks, you first need to cast your feed items to a SitelinkFeedItem. From there, you can get the final URLs. 

Here is a snippet of code you can add to yours that will give you the URLs
if(feedItem.getFeedType().getValue() == "SITELINK"){
   
SitelinkFeedItem sfi = (SitelinkFeedItem)feedItem;
   
System.out.println("Feed Item's Final Url: " + sfi.getSitelinkFinalUrls().getUrls(0));
}

Cheers,
Anthony
AdWords API Team

Karim Benna

unread,
Mar 16, 2016, 4:17:36 AM3/16/16
to AdWords API Forum
Hey Anthony 

Thank you for your post but i can't cast a SitelinkFeedItem from ExtensionFeedItem.

Kind regards

Karim Benna

unread,
Mar 16, 2016, 4:23:01 AM3/16/16
to AdWords API Forum
Oh i'm sorry i was importing the wrong version of SitelinkFeedItem the v201506. I hate this mistake. and thank you very much
Reply all
Reply to author
Forward
0 new messages