Hi Joel,
To add (or modify) SiteLinks for a campaign, you have to
- Check if there is an active SiteLinkExtension. If yes, remove it.
(Otherwise you will get
CANNOT_HAVE_MULTIPLE_SITELINKS_EXTENSIONS_PER_CAMPAIGN).
- Add a new SiteLinkExtension with the desired SiteLinks.
The sample code in C# to check if a campaign has active sitelinks is
given below:
long siteLinkExtensionId = -1;
// Get the campaign ad extension containing sitelinks.
CampaignAdExtensionSelector selector = new
CampaignAdExtensionSelector();
selector.campaignIds = new long[] { campaignId };
selector.statuses = new CampaignAdExtensionStatus[]
{ CampaignAdExtensionStatus.ACTIVE };
CampaignAdExtensionPage page = campaignExtensionService.get(selector);
if (page != null && page.entries != null) {
foreach (CampaignAdExtension extension in page.entries) {
if (extension.adExtension is SitelinksExtension) {
siteLinkExtensionId = extension.adExtension.id;
break;
}
}
}
If there are active sitelinks, then you can remove them as follows:
if (siteLinkExtensionId == -1) {
return; // No active sitelinks, so no need to remove.
}
CampaignAdExtension campaignAdExtension = new CampaignAdExtension();
campaignAdExtension.campaignId = campaignId;
campaignAdExtension.adExtension = new AdExtension(); // no need to
create a SiteLinksExtension here, just an AdExtension would do.
campaignAdExtension.adExtension.id = siteLinkExtensionId;
CampaignAdExtensionOperation operation = new
CampaignAdExtensionOperation();
operati...@operator = Operator.REMOVE;
operation.operand = campaignAdExtension;
CampaignAdExtensionReturnValue retVal =
campaignExtensionService.mutate(new CampaignAdExtensionOperation[]
{ operation });
Now you can add sitelinks as follows:
// create your sitelinks.
Sitelink siteLink = new Sitelink();
siteLink.displayText = "New albums";
siteLink.destinationUrl = "http://www.example.com/albums/new";
SitelinksExtension siteLinkExtension = new SitelinksExtension();
siteLinkExtension.sitelinks = new Sitelink[] {siteLink};
CampaignAdExtension campaignAdExtension = new CampaignAdExtension();
campaignAdExtension.adExtension = siteLinkExtension;
campaignAdExtension.campaignId = campaignId;
CampaignAdExtensionOperation operation = new
CampaignAdExtensionOperation();
operati...@operator = Operator.ADD;
operation.operand = campaignAdExtension;
CampaignAdExtensionReturnValue retVal =
campaignExtensionService.mutate(new CampaignAdExtensionOperation[]
{operation});
Hope this helps. Let me know if you have more questions.
Cheers,
Anash P. Oommen,
AdWords API Advisor.
On Aug 18, 1:13 am, joel <[email protected]> wrote:
> To follow-up, I can't even seem to remove SiteLinks using my
> methodology above. If I request the SiteLinks and then attempt a
> REMOVE operation, I get a NotEmptyError.EMPTY_LIST exception because
> my SitelinksExtension object has no Sitelinks attached. If I don't
> remove and simply call another ADD while specifying the existing
> SitelinksExtension Id, I get an INVALID_ADEXTENSION_ID errors. Lastly,
> if I simply try to ADD without setting the Id or removing, then I get
> a CANNOT_HAVE_MULTIPLE_SITELINKS_EXTENSIONS_PER_CAMPAIGN error.
>
> Here's a snippet of my code:
>
> CampaignAdExtensionServiceInterface extSvc =
> user.getService(AdWordsService.V201003.CAMPAIGN_AD_EXTENSION_SERVICE);
>
> try
> {
> CampaignAdExtensionSelector selector = new
> CampaignAdExtensionSelector();
> selector.setCampaignIds(new long[]
> { dbCampaign.getExternalIdAsLong() });
>
> // Get all campaign ad extensions.
> CampaignAdExtensionPage page = extSvc.get(selector);
>
> // Display campaign ad extensions.
> if (page.getEntries() != null &&
> page.getEntries().length > 0)
> {
> for (CampaignAdExtension campaignAdExtension :
> page.getEntries())
> {
> if
> (campaignAdExtension.getAdExtension().getAdExtensionType().equals("Sitelink
> sExtension"))
> {
> SitelinksExtension ext = new
> SitelinksExtension();
>
> ext.setId(campaignAdExtension.getAdExtension().getId());
>
> CampaignAdExtension extension = new
> CampaignAdExtension();
>
> extension.setCampaignId(dbCampaign.getExternalIdAsLong());
> extension.setAdExtension(ext);
>
> CampaignAdExtensionOperation operation =
> new CampaignAdExtensionOperation();
> operation.setOperand(extension);
> operation.setOperator(Operator.REMOVE);
>
> extSvc.mutate(new
> CampaignAdExtensionOperation[] { operation });
>
> break;
> }
> }
> }
> }
> catch (Exception e)
> {
> ServiceResult failure =
> GoogleUtils.populateFaultToServiceResult(e);
> results.put(dbCampaign, failure);
> continue;
> }
>
> // Now add the new SiteLinks
> List<CampaignAdExtensionOperation> lOperations = new
> ArrayList<CampaignAdExtensionOperation>();
>
> List<DBSiteLink> lSiteLinks = hmAdds.get(dbCampaign);
> for (DBSiteLink dbSiteLink : lSiteLinks)
> {
> Sitelink sl = new Sitelink();
> sl.setDestinationUrl(dbSiteLink.getDestinationUrl());
> sl.setDisplayText(dbSiteLink.getDisplayText());
>
> SitelinksExtension ext = new SitelinksExtension();
> ext.setSitelinks(new Sitelink[] { sl });
>
> CampaignAdExtension extension = new
> CampaignAdExtension();
>
> extension.setCampaignId(dbCampaign.getExternalIdAsLong());
> extension.setAdExtension(ext);
>
> CampaignAdExtensionOperation operation = new
> CampaignAdExtensionOperation();
> operation.setOperand(extension);
> operation.setOperator(Operator.ADD);
>
> lOperations.add(operation);
> }
>
> CampaignAdExtensionOperation[] operations =
> lOperations.toArray(new
> CampaignAdExtensionOperation[lOperations.size()]);
> extSvc.mutate(operations);
>
> Thanks for any help,
>
> Joel
>
> On Aug 17, 3:16 pm, joel <[email protected]> wrote:
>
>
>
> > I'm implementing an API tool to manage Sitelinks. The API objects only
> > provide an Id on the AdExtension object, and not for the Sitelink
> > object. Therefore I can't add/remove/update individual Sitelinks for a
> > Campaign, I need to delete the previous set of Sitelinks and re-add
> > all of them. Since I can't modify these, I also don't see the need for
> > the Id on the AdExtension object anyways. It seems as if the only way
> > to work with this API is to always have my code:
>
> > 1) GET/request any existing Sitelink AdExtension
> > 2) Delete the existing SiteLink AdExtension if one does exist
> > 3) Add a new one with my new list of Sitelinks
>
> > Is there a better way to do this? If not, can you please consider
> > adding Ids to the Sitelink objects so that those can be added/removed
> > individually.
>
> > Thanks,
>
> > Joel
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and discussion group:
http://adwordsapi.blogspot.com
http://groups.google.com/group/adwords-api
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
You received this message because you are subscribed to the Google
Groups "AdWords API Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en