Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Ads & AdGroups #33

Open
jvv8 opened this issue Oct 10, 2018 · 2 comments
Open

Adding Ads & AdGroups #33

jvv8 opened this issue Oct 10, 2018 · 2 comments

Comments

@jvv8
Copy link

jvv8 commented Oct 10, 2018

I see in the docs that it is possible to retrieve campaigns/adgroups/ads and then modify and save the existing object, but I don't see how to create a new adgroup or ad.
Am I missing something or is creating these entities not possible?
I tried something like this:

$text_ad = new ExpandedTextAd();
$text_ad->setHeadlinePart1($ad->headline1);
$text_ad->setHeadlinePart2($ad->headline2);
$text_ad->setDescription($ad->description);
$text_ad->setFinalUrls(array($ad->url));
$text_ad->setPath1($ad->url_path1);
$text_ad->setPath2($ad->url_path2);
$ad_group_ad = new AdGroupAd();
$ad_group_ad->setAdGroupId($ad->ad_group_id);
$ad_group_ad->setAd($text_ad);
$ad_group_ad->setStatus(AdGroupAdStatus::ENABLED);
$operation = new AdGroupAdOperation();
$operation->setOperand($ad_group_ad);
$operation->setOperator(Operator::ADD);
$operations = array();
$operations[] = $operation;
$service_collection = new ServiceCollection($this->api->service(AdGroupAdService::class), $operations);
$result = $service_collection->save();

But this returns the error:

Call to undefined method Edujugon\GoogleAds\Services\Service::mutate()
vendor/edujugon/laravel-google-ads/src/Services/ServiceCollection.php line 135
@jvv8
Copy link
Author

jvv8 commented Oct 10, 2018

I played around with the ServiceCollection class and added a new method:

public function add(){
    $operations = [];
    $this->items->each(function ($item) use(&$operations) {
        $operation = $this->setOperator();
        $operation->setOperand($item);
        $operation->setOperator('ADD');
        $operations[] = $operation;
    });
    if(empty($operations))
        return false;
    return $this->adWordsServices->mutate($operations)->getValue();
}

And then in my app code

$this->api = new GoogleAds();
....
$text_ad = new ExpandedTextAd();
$text_ad->setHeadlinePart1($ad->headline1);
$text_ad->setHeadlinePart2($ad->headline2);
$text_ad->setDescription($ad->description);
$text_ad->setFinalUrls(array($ad->url));
$text_ad->setPath1($ad->url_path1);
$text_ad->setPath2($ad->url_path2);
$ad_group_ad = new AdGroupAd();
$ad_group_ad->setAdGroupId($ad->ad_group_id);
$ad_group_ad->setAd($text_ad);
$ad_group_ad->setStatus(AdGroupAdStatus::ENABLED);

$service = $this->api->service(AdGroupAdService::class)->getService();
$service_collection = new ServiceCollection($service, ['ad'=>$ad_group_ad]);
$result = $service_collection->add();

Seems to work, but there maybe a better way of executing this.

@Edujugon
Copy link
Owner

Hi @jvv8 ,

Thanks for bringing up this topic.

This package was created because after working on a project for my previous company I decided to make it public so that It could be helpful for other developers. The company decided to stop that project so that's why that functionaly is not there.

I think it should be there so as soon as I get some free time I will try to add it. I can not provide an ETA since I'm currently really busy but if you are up for a PR, it will be more than welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants