I have been changing the distinations URL of my AdWords ads using a little
program I wrote in C#. However, then I hit ads of type image ad with
images, the program throws an exception. Appearently I need to set
operand.ad.image.mediaId. My program changes the URL by retrieving the ads
in question, changing the URL, deleting them on the AdWords server and
finally adding them again (with the new URL). For text ads it works
perfectly.
I have read somethere that if I only retrieve field from the generic ad
type (Ad) the program would execute without errors. I select only Id and
Url via AWQL, so I though I was in the clear. To get the ads I use an
AdGroupAdService to make an AdGroupAdPage. I really like to have an unified
solution that can work for any type of ad, however, a solution just working
for image ads is also welcome.
Have a nice day.
Some source code (C#). I appreciate any kind of suggestions or comments.
public void Remake(string campaignName, int campaignElementId, int
contentId)
{
long campaignId = GetCampaignId(campaignName);
List<long> groupIds = GetAdGroupIds(campaignId);
StringBuilder query = new StringBuilder("SELECT Id, Url WHERE
AdGroupId IN [ ");
bool isFirst = true;
foreach (long groupId in groupIds)
{
if (!isFirst) query.Append(", ");
query.Append(groupId);
isFirst = false;
}
query.Append(" ]");
AdGroupAdService service =
(AdGroupAdService)user.GetService(AdWordsService.v201209.AdGroupAdService);
AdGroupAdPage page = service.query(query.ToString());
List<AdGroupAd> ads = new List<AdGroupAd>();
foreach (AdGroupAd ad in page.entries)
{
ad.ad.url = NewUrl(ad, campaignElementId, contentId);
ads.Add(ad);
}
UpdateAds(ads);
}
private void UpdateAds(List<AdGroupAd> ads)
{
AdGroupAdService service =
(AdGroupAdService)user.GetService(AdWordsService.v201209.AdGroupAdService);
List<AdGroupAdOperation> removing = new
List<AdGroupAdOperation>();
foreach (AdGroupAd ad in ads)
{
AdGroupAdOperation operation = new AdGroupAdOperation();
operation.@operator = Operator.REMOVE;
operation.operand = ad;
removing.Add(operation);
}
List<AdGroupAdOperation> adding = new
List<AdGroupAdOperation>();
foreach (AdGroupAd ad in ads)
{
AdGroupAdOperation operation = new AdGroupAdOperation();
operation.@operator = Operator.ADD;
operation.operand = ad;
adding.Add(operation);
}
AdGroupAdReturnValue retValRemoving =
service.mutate(removing.ToArray());
AdGroupAdReturnValue retValAdding =
service.mutate(adding.ToArray());
}
--
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
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
---
You received this message because you are subscribed to the Google Groups
"AdWords API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.