/
AddTrial.php
executable file
·167 lines (147 loc) · 6.16 KB
/
AddTrial.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\AdsApi\Examples\AdWords\v201809\CampaignManagement;
require __DIR__ . '/../../../../vendor/autoload.php';
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201809\cm\CampaignTrialTrafficSplitType;
use Google\AdsApi\AdWords\v201809\cm\Operator;
use Google\AdsApi\AdWords\v201809\cm\Predicate;
use Google\AdsApi\AdWords\v201809\cm\PredicateOperator;
use Google\AdsApi\AdWords\v201809\cm\Selector;
use Google\AdsApi\AdWords\v201809\cm\Trial;
use Google\AdsApi\AdWords\v201809\cm\TrialAsyncErrorService;
use Google\AdsApi\AdWords\v201809\cm\TrialOperation;
use Google\AdsApi\AdWords\v201809\cm\TrialService;
use Google\AdsApi\AdWords\v201809\cm\TrialStatus;
use Google\AdsApi\Common\OAuth2TokenBuilder;
/**
* This example shows how to create a trial and wait for it to complete.
* See the Campaign Drafts and Experiments guide for more information:
* https://developers.google.com/adwords/api/docs/guides/campaign-drafts-experiments
*/
class AddTrial
{
const MAX_POLL_ATTEMPTS = 5;
const POLL_FREQUENCY_SECONDS = 30;
const DRAFT_ID = 'INSERT_DRAFT_ID_HERE';
const BASE_CAMPAIGN_ID = 'INSERT_BASE_CAMPAIGN_ID_HERE';
public static function runExample(
AdWordsServices $adWordsServices,
AdWordsSession $session,
$draftId,
$baseCampaignId
) {
$trialService = $adWordsServices->get($session, TrialService::class);
$trialAsynErrorService = $adWordsServices->get($session, TrialAsyncErrorService::class);
// Create a trial.
$trial = new Trial();
$trial->setDraftId($draftId);
$trial->setBaseCampaignId($baseCampaignId);
$trial->setName('Test Trial #' . uniqid());
$trial->setTrafficSplitPercent(50);
$trial->setTrafficSplitType(
CampaignTrialTrafficSplitType::RANDOM_QUERY
);
// Create a trial operation and add it to the operations list.
$operations = [];
$operation = new TrialOperation();
$operation->setOperand($trial);
$operation->setOperator(Operator::ADD);
$operations[] = $operation;
// Create the trial on the server.
$trial = $trialService->mutate($operations)->getValue()[0];
$selector = new Selector();
$selector->setFields(
['Id', 'Status', 'BaseCampaignId', 'TrialCampaignId']
);
$selector->setPredicates(
[new Predicate('Id', PredicateOperator::IN, [$trial->getId()])]
);
// Since creating a trial is asynchronous, we have to poll it to wait for it
// to finish.
$pollAttempts = 0;
$isPending = true;
$trial = null;
do {
$sleepSeconds = self::POLL_FREQUENCY_SECONDS * pow(2, $pollAttempts);
printf("Sleeping %d seconds...\n", $sleepSeconds);
sleep($sleepSeconds);
$trial = $trialService->get($selector)->getEntries()[0];
printf(
"Trial ID %d has status '%s'.\n",
$trial->getId(),
$trial->getStatus()
);
$pollAttempts++;
$isPending = ($trial->getStatus() === TrialStatus::CREATING) ? true : false;
} while ($isPending && $pollAttempts <= self::MAX_POLL_ATTEMPTS);
if ($trial->getStatus() === TrialStatus::ACTIVE) {
// The trial creation was successful.
printf(
"Trial created with ID %d and trial campaign ID %d\n",
$trial->getId(),
$trial->getTrialCampaignId()
);
} elseif ($trial->getStatus() === TrialStatus::CREATION_FAILED) {
// The trial creation failed, and errors can be fetched from the
// TrialAsyncErrorService.
$selector = new Selector();
$selector->setFields(['TrialId', 'AsyncError']);
$selector->setPredicates(
[new Predicate('TrialId', PredicateOperator::IN, [$trial->getId()])]
);
$errors = $trialAsynErrorService->get($selector)->getEntries();
if (count($errors) === 0) {
printf(
"Could not retrieve errors for the trial with ID %d\n",
$trial->getId()
);
} else {
printf("Could not create trial due to the following errors:\n");
$i = 0;
foreach ($errors as $error) {
printf("Error #%d: %s\n", $i++, $error->getAsyncError());
}
}
} else {
// Most likely, the trial is still being created. You can continue
// polling, but we have limited the number of attempts in the example.
printf(
"Timed out waiting to create trial from draft with ID %d with base campaign with ID %d\n",
$draftId,
$baseCampaignId
);
}
}
public static function main()
{
// Generate a refreshable OAuth2 credential for authentication.
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();
// Construct an API session configured from a properties file and the
// OAuth2 credentials above.
$session = (new AdWordsSessionBuilder())->fromFile()->withOAuth2Credential($oAuth2Credential)->build();
self::runExample(
new AdWordsServices(),
$session,
intval(self::DRAFT_ID),
intval(self::BASE_CAMPAIGN_ID)
);
}
}
AddTrial::main();