Edujugon / laravel-google-ads

Google Adwords API for Laravel
MIT License
68 stars 23 forks source link

Adding Ads & AdGroups #33

Open jvv8 opened 6 years ago

jvv8 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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.