Creating custom affinity audience

240 views
Skip to first unread message

Aekansh Partani

unread,
Dec 5, 2019, 6:18:23 AM12/5/19
to AdWords API and Google Ads API Forum
Is there any example on how can we create custom affinity audience using Adwords API using PHP?

Google Ads API Forum Advisor Prod

unread,
Dec 5, 2019, 1:03:22 PM12/5/19
to aekan...@gmail.com, adwor...@googlegroups.com
Hello Aekansh, 

I'm afraid, we do not have the PHP code sample to create Custom Affinity audience. Please find the code snippet in Java as a reference.

CustomAffinityServiceInterface listService = adWordsServices.get(session, CustomAffinityServiceInterface.class);
CustomAffinity audience = new CustomAffinity();
audience.setStatus(CustomAffinityStatus.ENABLED);
audience.setName("Custom Created Intent_47");
audience.setType(CustomAffinityType.CUSTOM_INTENT);

CustomAffinityOperation operation = new CustomAffinityOperation();
operation.setOperand(audience);
operation.setOperator(Operator.ADD);

CustomAffinityOperation[] operations = new CustomAffinityOperation[] {operation};
CustomAffinityReturnValue result = listService.mutate(operations);

You may refer this and try it out in PHP. Let me know if you have any issues, I'll be happy to take a look.

Thanks,
Bharani, Google Ads API Team


ref:_00D1U1174p._5001UODtta:ref

Aekansh Partani

unread,
Dec 5, 2019, 11:32:17 PM12/5/19
to AdWords API and Google Ads API Forum
Thanks for your reply.
I used this in PHP and got Call to undefined method Google\AdsApi\AdWords\v201809\rm\CustomAffinity::setStreamContext()

$customAffinityService = $adWordsServices->get(sessionBuilder(), CustomAffinity::class);

$audience = new CustomAffinity();
$audience->setStatus(CustomAffinityStatus::ENABLED);
$audience->setName("Custom Created Intent");
$audience->setType(CustomAffinityType::CUSTOM_INTENT);

$customAffinityOperation = new CustomAffinityOperation();
$customAffinityOperation->setOperand($audience);
$customAffinityOperation->setOperator(Operator::ADD);

$operations = [];
$operations[] = $customAffinityOperation;
$result = $customAffinityService->mutate($operations);

Google Ads API Forum Advisor Prod

unread,
Dec 6, 2019, 11:24:20 AM12/6/19
to aekan...@gmail.com, adwor...@googlegroups.com
Hi Aekansh, 

Your code snippet looks fine to me. It looks like it is mostly a client library specific issue and a similar issue has been reported on the Github Issue Tracker. Could you please check the solution shared? If you're still having an issue, could you please open a new thread on the issue tracker and the library owners will be able to take a look. 
Message has been deleted

Aekansh Partani

unread,
Dec 9, 2019, 6:38:22 AM12/9/19
to AdWords API and Google Ads API Forum
Thanks for the help. It seems issue was with my code.

        $customAffinityService = $adWordsServices->get(sessionBuilder(), CustomAffinityService::class);

I was using CustomAffinity instead of CustomAffinityService.

Also is there is any way we can help adding examples to google adwords library as that might help someone.

Google Ads API Forum Advisor Prod

unread,
Dec 9, 2019, 3:12:35 PM12/9/19
to aekan...@gmail.com, adwor...@googlegroups.com
Hi Aekansh, 

I'm glad that the issue has been resolved. Could you point me to the UI screenshot where you're trying to add the audience? If you're looking to add the affinity categories, you may use the AdGroupCriterionService.mutate() or CampaignCriterionService.mutate() to target them to the campaigns or ad groups. You may refer to the Add Campaign Targeting PHP code sample as a reference. 

You can submit a pull request to the client library and the library owners will take a look. 
Reply all
Reply to author
Forward
0 new messages