1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SchulzeFelix\AdWords; |
4
|
|
|
|
5
|
|
|
use Google\AdsApi\AdWords\v201809\cm\ApiException; |
6
|
|
|
use Google\AdsApi\AdWords\v201809\cm\Language; |
7
|
|
|
use Google\AdsApi\AdWords\v201809\cm\Location; |
8
|
|
|
use Google\AdsApi\AdWords\v201809\cm\NetworkSetting; |
9
|
|
|
use Google\AdsApi\AdWords\v201809\cm\Paging; |
10
|
|
|
use Google\AdsApi\AdWords\v201809\o\AttributeType; |
11
|
|
|
use Google\AdsApi\AdWords\v201809\o\IdeaTextFilterSearchParameter; |
12
|
|
|
use Google\AdsApi\AdWords\v201809\o\IdeaType; |
13
|
|
|
use Google\AdsApi\AdWords\v201809\o\LanguageSearchParameter; |
14
|
|
|
use Google\AdsApi\AdWords\v201809\o\LocationSearchParameter; |
15
|
|
|
use Google\AdsApi\AdWords\v201809\o\NetworkSearchParameter; |
16
|
|
|
use Google\AdsApi\AdWords\v201809\o\RelatedToQuerySearchParameter; |
17
|
|
|
use Google\AdsApi\AdWords\v201809\o\TargetingIdeaSelector; |
18
|
|
|
use Google\AdsApi\AdWords\v201809\o\TargetingIdeaService; |
19
|
|
|
|
20
|
|
|
class AdWordsService |
21
|
|
|
{ |
22
|
|
|
const PAGE_LIMIT = 700; |
23
|
|
|
|
24
|
|
|
const MAX_RETRIES = 10; |
25
|
|
|
|
26
|
|
|
/** @var TargetingIdeaService */ |
27
|
|
|
protected $targetingIdeaService; |
28
|
|
|
|
29
|
|
|
public function __construct(TargetingIdeaService $targetingIdeaService) |
30
|
|
|
{ |
31
|
|
|
$this->targetingIdeaService = $targetingIdeaService; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Query the Google AdWords TargetingIdeaService with given parameters. |
36
|
|
|
* |
37
|
|
|
* @param array $keywords |
38
|
|
|
* @param $requestType |
39
|
|
|
* @param $language |
40
|
|
|
* @param $location |
41
|
|
|
* @param bool $withTargetedMonthlySearches |
42
|
|
|
* @param $included |
43
|
|
|
* @param $excluded |
44
|
|
|
* |
45
|
|
|
* @throws ApiException |
46
|
|
|
* |
47
|
|
|
* @return \Google\AdsApi\AdWords\v201809\o\TargetingIdeaPage |
48
|
|
|
*/ |
49
|
|
|
public function performQuery(array $keywords, $requestType, $language = null, $location = null, $withTargetedMonthlySearches = false, $included = null, $excluded = null) |
50
|
|
|
{ |
51
|
|
|
|
52
|
|
|
// Create selector. |
53
|
|
|
$selector = new TargetingIdeaSelector(); |
54
|
|
|
$selector->setRequestType($requestType); |
55
|
|
|
$selector->setIdeaType(IdeaType::KEYWORD); |
56
|
|
|
$selector->setRequestedAttributeTypes($this->getRequestedAttributeTypes($withTargetedMonthlySearches)); |
57
|
|
|
$selector->setPaging(new Paging(0, self::PAGE_LIMIT)); |
58
|
|
|
$selector->setSearchParameters($this->getSearchParameters($keywords, $language, $location, $included, $excluded)); |
59
|
|
|
|
60
|
|
|
return (new ExponentialBackoff(10))->execute(function () use ($selector) { |
61
|
|
|
return $this->targetingIdeaService->get($selector); |
62
|
|
|
}); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return TargetingIdeaService |
67
|
|
|
*/ |
68
|
|
|
public function getTargetingIdeaService() |
69
|
|
|
{ |
70
|
|
|
return $this->targetingIdeaService; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param bool $withTargetedMonthlySearches |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
private function getRequestedAttributeTypes($withTargetedMonthlySearches = false) |
79
|
|
|
{ |
80
|
|
|
if ($withTargetedMonthlySearches) { |
81
|
|
|
return [ |
82
|
|
|
AttributeType::KEYWORD_TEXT, |
83
|
|
|
AttributeType::SEARCH_VOLUME, |
84
|
|
|
AttributeType::COMPETITION, |
85
|
|
|
AttributeType::AVERAGE_CPC, |
86
|
|
|
AttributeType::TARGETED_MONTHLY_SEARCHES, |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return [ |
91
|
|
|
AttributeType::KEYWORD_TEXT, |
92
|
|
|
AttributeType::SEARCH_VOLUME, |
93
|
|
|
AttributeType::COMPETITION, |
94
|
|
|
AttributeType::AVERAGE_CPC, |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $keywords |
100
|
|
|
* @param $languageId |
101
|
|
|
* @param $locationId |
102
|
|
|
* @param $included |
103
|
|
|
* @param $excluded |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
private function getSearchParameters(array $keywords, $languageId, $locationId, $included, $excluded) |
108
|
|
|
{ |
109
|
|
|
$searchParameters = []; |
110
|
|
|
|
111
|
|
|
//Create Language Parameter |
112
|
|
|
if (! is_null($languageId)) { |
113
|
|
|
$languageParameter = new LanguageSearchParameter(); |
114
|
|
|
$language = new Language(); |
115
|
|
|
$language->setId($languageId); |
116
|
|
|
$languageParameter->setLanguages([$language]); |
117
|
|
|
$searchParameters[] = $languageParameter; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
//Create Location Parameter |
121
|
|
|
if (! is_null($locationId)) { |
122
|
|
|
$locationParameter = new LocationSearchParameter(); |
123
|
|
|
$location = new Location(); |
124
|
|
|
$location->setId($locationId); |
125
|
|
|
$locationParameter->setLocations([$location]); |
126
|
|
|
$searchParameters[] = $locationParameter; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
//Network Settings |
130
|
|
|
$networkSetting = new NetworkSetting(); |
131
|
|
|
$networkSetting->setTargetGoogleSearch(true); |
132
|
|
|
$networkSetting->setTargetSearchNetwork(false); |
133
|
|
|
$networkSetting->setTargetContentNetwork(false); |
134
|
|
|
$networkSetting->setTargetPartnerSearchNetwork(false); |
135
|
|
|
|
136
|
|
|
$networkSearchParameter = new NetworkSearchParameter(); |
137
|
|
|
$networkSearchParameter->setNetworkSetting($networkSetting); |
138
|
|
|
$searchParameters[] = $networkSearchParameter; |
139
|
|
|
|
140
|
|
|
// Create related to query search parameter. |
141
|
|
|
$relatedToQuerySearchParameter = new RelatedToQuerySearchParameter(); |
142
|
|
|
$relatedToQuerySearchParameter->setQueries($keywords); |
143
|
|
|
$searchParameters[] = $relatedToQuerySearchParameter; |
144
|
|
|
|
145
|
|
|
if (! is_null($included) || ! is_null($excluded)) { |
146
|
|
|
$ideaTextFilterSearchParameter = new IdeaTextFilterSearchParameter(); |
147
|
|
|
if (! is_null($included)) { |
148
|
|
|
$ideaTextFilterSearchParameter->setIncluded($included); |
149
|
|
|
} |
150
|
|
|
if (! is_null($excluded)) { |
151
|
|
|
$ideaTextFilterSearchParameter->setExcluded($excluded); |
152
|
|
|
} |
153
|
|
|
$searchParameters[] = $ideaTextFilterSearchParameter; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $searchParameters; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|