edujugon/laravel-google-ads
Composer 安装命令:
composer require edujugon/laravel-google-ads
包简介
Google Adwords API for Laravel
README 文档
README
Simple and easy to use API for your Google Adwords.
Installation
Type in console:
composer require edujugon/laravel-google-ads
Laravel 5.5 or higher?
Then you don't have to either register or add the alias, this package uses Package Auto-Discovery's feature, and should be available as soon as you install it via Composer.
(Only for Laravel 5.4 or minor) Register the GoogleAds service by adding it to the providers array.
'providers' => array(
...
Edujugon\GoogleAds\Providers\GoogleAdsServiceProvider::class
)
(Only for Laravel 5.4 or minor) Let's add the Alias facade, add it to the aliases array.
'aliases' => array(
...
'GoogleAds' => Edujugon\GoogleAds\Facades\GoogleAds::class,
)
Publish the package's configuration file to the application's own config directory.
php artisan vendor:publish --provider="Edujugon\GoogleAds\Providers\GoogleAdsServiceProvider" --tag="config"
The above command will generate a new file under your laravel app config folder called google-ads.php
Configuration
Update the google-ads.php file with your data.
'env' => 'test',
'production' => [
'developerToken' => "YOUR-DEV-TOKEN",
'clientCustomerId' => "CLIENT-CUSTOMER-ID",
'userAgent' => "YOUR-NAME",
'clientId' => "CLIENT-ID",
'clientSecret' => "CLIENT-SECRET",
'refreshToken' => "REFRESH-TOKEN"
],
'test' => [
'developerToken' => "YOUR-DEV-TOKEN",
'clientCustomerId' => "CLIENT-CUSTOMER-ID",
'userAgent' => "YOUR-NAME",
'clientId' => "CLIENT-ID",
'clientSecret' => "CLIENT-SECRET",
'refreshToken' => "REFRESH-TOKEN"
],
'env' key accepts one of the following values: test / production
Generate refresh token
Notice that it will take the
clientIDandclientSecretfromgoogle-ads.phpconfig file based on theenvvalue.
Type in console:
php artisan googleads:token:generate
- Visit the URL it shows, grant access to your app and input the access token in console.
- Then copy the fresh token in
google-ads.phpconfig file.
Remember to copy that token in the correct section (test/production).Depends on your
envvalue.
Usage samples
Instance the main wrapper class:
$ads = new GoogleAds();
Do not forget to put at the top of the file the use statement:
use Edujugon\GoogleAds\GoogleAds;
All needed configuration data is took from google-ads.php config file. But you always may pass new values on the fly if required.
You may override the default environment value calling the env method:
$ads->env('test');
Also, you may get the env value by getEnv method:
$ads->getEnv();
If need to override the oAuth details, just call the oAuth method like so:
$ads->oAuth([
'clientId' => 'test',
'clientSecret' => 'test',
'refreshToken' => 'TEST'
]);
Same with session. If need to override the default values on the fly, just do it by calling session method:
$ads->session([
'developerToken' => 'token',
'clientCustomerId' => 'id'
]);
All the above methods can be chained as follows:
$ads->env('test')
->oAuth([
'clientId' => 'test',
'clientSecret' => 'test',
'refreshToken' => 'TEST'
])
->session([
'developerToken' => 'token',
'clientCustomerId' => 'id'
]);
Google Services
For Google Ads Services you only have to call the service method:
$ads->service(CampaignService::class);
or
$ads->service(AdGroupService::class);
or
$ads->service(AdGroupAdService::class);
or Any google ads services available under Google\AdsApi\AdWords\v201809\cm folder.
Also you can use the global helper in order the get an instance of Service.
$service = google_service(CampaignService::class)
To retrieve a list of campaigns, do like follows:
$ads->service(CampaignService::class)
->select(['Id', 'Name', 'Status', 'ServingStatus', 'StartDate', 'EndDate'])
->get();
Notice the method
selectis required and you have to use it in order to set the fields you wanna get from the campaign.
If need to add a condition to your search you can use the where method like follows:
$ads->service(CampaignService::class)
->select(['Id', 'Name', 'Status', 'ServingStatus', 'StartDate', 'EndDate'])
->where('Id IN [752331963,795625088]')
->get();
or
$ads->service(CampaignService::class)
->select(['Id', 'Name', 'Status', 'ServingStatus', 'StartDate', 'EndDate'])
->where('Id = 752331963')
->get();
Notice! You may also set more than one condition. Do so calling
wheremethod as many times as you need.
Available Operators:
= | != | > | >= | < | <= | IN | NOT_IN | STARTS_WITH | STARTS_WITH_IGNORE_CASE |
CONTAINS | CONTAINS_IGNORE_CASE | DOES_NOT_CONTAIN | DOES_NOT_CONTAIN_IGNORE_CASE |
CONTAINS_ANY | CONTAINS_NONE | CONTAINS_ALL
If need to limit your search you may use limit method:
$ads->service(CampaignService::class)
->select(['Id', 'Name', 'Status', 'ServingStatus', 'StartDate', 'EndDate'])
->limit(5)
->get();
Also you can order by a field:
$ads->service(CampaignService::class)
->select(['Id', 'Name', 'Status', 'ServingStatus', 'StartDate', 'EndDate'])
->orderBy('Name')
->limit(5)
->get();
Notice that the get method returns an instance of ServiceCollection.
That custom collection has its own methods.
Once you have the collection, you can again filter with the where method
$campaignService = $ads->service(CampaignService::class);
$results = $campaignService->select('CampaignId','CampaignName')->get();
//You can also add any where condition on the list.
$campaign = $results->where('id',1341312);
Also you can call the set method to change any value
$campaign = $results->where('id',$this->testedCampaignId)->set('name','hello !!');
Finally you can persist those changes with the save method:
$campaign = $campaign->save();
Save method returns an array of updated elements or false if nothing updated.
Important!! notice that it will persist all elements that are in the collection.
You can get the list as illuminate collection simply calling items method.
Google Reports
To start with google reporting just call report method from the main wrapper:
$report = $ads->report();
or use the global helper like follows:
$report = google_report();
It will return an instance of Edujugon\GoogleAds\Reports\Report
Now, you have a set of method to prepare the google ads report:
$obj = $ads->report()
->from('CRITERIA_PERFORMANCE_REPORT')
->during('20170101','20170210')
->where('CampaignId = 752331963')
->select('CampaignId','AdGroupId','AdGroupName','Id', 'Criteria', 'CriteriaType','Impressions', 'Clicks', 'Cost', 'UrlCustomParameters')
->getAsObj();
In the above methods, the mandatory ones are from and select
Notice that in
duringmethod you have to pass the dates as a string like YearMonthDay
You may also want to set more than one condition. Use Where clause as many times as you need like follows:
$obj = $ads->report()
->from('CRITERIA_PERFORMANCE_REPORT')
->where('Clicks > 10')
->where('Cost > 10')
->where('Impressions > 1')
->select('CampaignId','AdGroupId','AdGroupName','Id', 'Criteria', 'CriteriaType','Impressions', 'Clicks', 'Cost', 'UrlCustomParameters')
->getAsObj();
Available Operators:
= | != | > | >= | < | <= | IN | NOT_IN | STARTS_WITH | STARTS_WITH_IGNORE_CASE |
CONTAINS | CONTAINS_IGNORE_CASE | DOES_NOT_CONTAIN | DOES_NOT_CONTAIN_IGNORE_CASE |
CONTAINS_ANY | CONTAINS_NONE | CONTAINS_ALL
Want to exclude any field? Just do it like follows:
$obj = $ads->report()
->from('SHOPPING_PERFORMANCE_REPORT')
->select(\Edujugon\GoogleAds\Facades\GoogleAds::fields()->of('SHOPPING_PERFORMANCE_REPORT')->asList())
->except('SearchImpressionShare','ExternalConversionSource','Ctr','Cost','Date','Week','Year','AverageCpc','Clicks','ClickType','ConversionCategoryName','ConversionTrackerId','ConversionTypeName')
->getAsObj();
If want to see the retrieve items, just get so by result property of the object returned:
$items = $obj->result;
Notice that it is a Collection. So you have all collection methods available.
If need the report in another format, just call format method before getting the report:
$string = $ads->report()
->format('CSVFOREXCEL')
->select('CampaignId','AdGroupId','AdGroupName','Id', 'Criteria', 'CriteriaType','Impressions', 'Clicks', 'Cost', 'UrlCustomParameters')
->from('CRITERIA_PERFORMANCE_REPORT')
->getAsString();
To see the available report formats:
$ads->report()->getFormats()
To see what fields are available for a specific report type you can do like follows:
$fields = $ads->report()->from('CRITERIA_PERFORMANCE_REPORT')->getFields();
If want to know what report types are available, just do like follow:
$ads->report()->getTypes();
There are 3 output formats for the report. It can be as object, as stream, as string.
getAsString();
getStream();
getAsObj();
Also you can save the report in a file:
$saved = $ads->report()
->select('CampaignId','AdGroupId','AdGroupName','Id', 'Criteria', 'CriteriaType','Impressions', 'Clicks', 'Cost', 'UrlCustomParameters')
->from('CRITERIA_PERFORMANCE_REPORT')
->saveToFile($filePath)
The above code will create a file in the passed path returning true if everything was fine.
API Documentation
edujugon/laravel-google-ads 适用场景与选型建议
edujugon/laravel-google-ads 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 425.34k 次下载、GitHub Stars 达 66, 最近一次更新时间为 2017 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「google」 「adwords」 「laravel」 「ads」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 edujugon/laravel-google-ads 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 edujugon/laravel-google-ads 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 edujugon/laravel-google-ads 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
AdWords PHP API Client
Google Ads API for Laravel forked from nikolajlovenhardt/laravel-google-ads
AdWords PHP API Client
Laravel wrapper for the AdWords Targeting Idea Service
Google Shopping Product Feed for PHP
Contao Extension to add conversion tracking codes, such as from google adwords or facebook to a specific page.
统计信息
- 总下载量: 425.34k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 66
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-02-15