GetCampaigns.php
4.56 KB
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
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Campaigns;
use App\Models\Pivots\DictionaryCampaign;
use App\Service\Requests\DirectRequest;
use Illuminate\Support\Facades\Log;
class GetCampaigns extends DirectRequest
{
protected $timestamp;
function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
function handle($response)
{
try {
$campaigns_data = [];
foreach ($response['result']['Campaigns'] as $campaign_data) {
$data = [
'external_id' => $campaign_data['Id'],
'token' => $this->getToken()->id,
'name' => $campaign_data['Name'],
'time_targeting' => json_encode($campaign_data['TimeTargeting']),
'negative_keywords' => json_encode($campaign_data['NegativeKeywords']['Items'] ?? []),
'blocked_ips' => json_encode($campaign_data['BlockedIps']['Items'] ?? []),
'excluded_sites' => json_encode($campaign_data['ExcludedSites']['Items'] ?? []),
'daily_budget' => json_encode($campaign_data['DailyBudget'] ?? []),
'text_campaign_strategy_search' => $campaign_data['TextCampaign']['BiddingStrategy']['Search']['BiddingStrategyType'],
'text_campaign_strategy_network' => $campaign_data['TextCampaign']['BiddingStrategy']['Network']['BiddingStrategyType'],
'settings' => json_encode($campaign_data['TextCampaign']['Settings'] ?? []),
'counter_ids' => json_encode($campaign_data['TextCampaign']['CounterIds']['Items'] ?? []),
'relevant_keywords_setting_budget_percent' => $campaign_data['TextCampaign']['RelevantKeywords']['BudgetPercent'] ?? null,
'relevant_keywords_setting_optimize_goal_id' => $campaign_data['TextCampaign']['RelevantKeywords']['OptimizeGoalId'] ?? null,
'attribution_model' => $campaign_data['TextCampaign']['AttributionModel'],
'priority_goals' => json_encode($campaign_data['TextCampaign']['PriorityGoals'] ?? []),
'updated_self' => null,
'updated_children' => null,
];
$campaigns_data[] = $data;
}
if ($this->getToken()->isMain()) {
Campaigns::upsert($campaigns_data, [
'external_id'
], [
'token',
'name',
'time_targeting',
'negative_keywords',
'blocked_ips',
'excluded_sites',
'daily_budget',
'text_campaign_strategy_search',
'text_campaign_strategy_network',
'settings',
'counter_ids',
'relevant_keywords_setting_budget_percent',
'relevant_keywords_setting_optimize_goal_id',
'attribution_model',
'priority_goals',
'updated_self',
'updated_children',
]);
} else {
foreach ($campaigns_data as $campaign_data) {
$dictionaryCampaign = DictionaryCampaign::synchronized()
->find($campaign_data['external_id']);
if (!$dictionaryCampaign)
continue;
$dictionaryCampaign->update(
$dictionaryCampaign::copyPropertyInCampaign($campaign_data)
);
}
}
} catch (\Exception $e) {
Log::debug($e);
}
}
private function requestPrepare($filter)
{
$this->setService('campaigns');
$this->setMethod('get');
$params = [
'SelectionCriteria' => [
'Types' => ["TEXT_CAMPAIGN"]
],
"FieldNames" => [
"Id", "Name", "TimeTargeting", "TimeZone", "NegativeKeywords", "BlockedIps", "ExcludedSites", "DailyBudget"
],
"TextCampaignFieldNames" => [
"BiddingStrategy", "Settings", "CounterIds", "RelevantKeywords", "PriorityGoals", "AttributionModel"
]
];
if (!empty($filter['ids'])) {
$params['SelectionCriteria']['Ids'] = $filter['ids'];
}
$this->setParams($params);
}
}