Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 36af68d

Browse files
committedNov 27, 2019
Saving some data from AdWords
1 parent 55e8d8e commit 36af68d

File tree

9 files changed

+336
-94
lines changed

9 files changed

+336
-94
lines changed
 

‎bin/etl.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace AdsWarehouse;
44

5-
use AdsWarehouse\ETL\GoogleAnalytics;
5+
use AdsWarehouse\ETL\AdWords;
6+
use AdsWarehouse\ETL\ETL;
67
use Google_Client;
78
use Google_Service_Analytics;
89
use Google_Service_AnalyticsReporting;
@@ -17,16 +18,19 @@
1718

1819
$pdo = new PDO(getenv('POSTGRES_DSN'));
1920
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::ERRMODE_EXCEPTION);
20-
21-
$context = new Context();
22-
$context->warehouse = new Warehouse\Pdo($pdo);
21+
$warehouse = new Warehouse\Pdo($pdo);
2322

2423
$google_client = new Google_Client();
2524
$google_client->setApplicationName('Ads Warehouse');
2625
$google_client->setAuthConfig($basedir . DIRECTORY_SEPARATOR . getenv('GOOGLE_CREDENTIALS'));
2726
$google_client->setScopes([Google_Service_Analytics::ANALYTICS_READONLY]);
2827

2928
$analytics = new Google_Service_AnalyticsReporting($google_client);
30-
$analytics_etl = new GoogleAnalytics($context->warehouse, $analytics, getenv('GA_VIEW_ID'));
3129

32-
$analytics_etl->load();
30+
$elt = [
31+
new AdWords($warehouse, $analytics, getenv('GA_VIEW_ID')),
32+
];
33+
34+
array_walk($elt, function (ETL $etl): void {
35+
$etl->load();
36+
});

‎composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"monolog/monolog": "^2.0",
99
"firebase/php-jwt": "^5.0",
1010
"webonyx/graphql-php": "^0.13.8",
11-
"google/apiclient": "^2.4"
11+
"google/apiclient": "^2.4",
12+
"ramsey/uuid": "^3.8"
1213
},
1314
"require-dev": {
1415
"swoole/ide-helper": "^4.4",
@@ -30,7 +31,6 @@
3031
}
3132
},
3233
"scripts": {
33-
"up": "docker-compose up",
3434
"etl": "docker-compose exec -T server php bin/etl.php"
3535
}
3636
}

‎composer.lock

Lines changed: 186 additions & 59 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Ad/Ad.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,35 @@
22

33
namespace AdsWarehouse\Ad;
44

5+
use DateTime;
6+
57
class Ad
68
{
79
/** @var string */
810
public $id;
911
/** @var string */
1012
public $name;
13+
/** @var float */
14+
public $cost;
15+
/** @var int */
16+
public $impressions;
17+
/** @var int */
18+
public $clicks;
19+
/** @var float */
20+
public $cpm;
21+
/** @var float */
22+
public $cpc;
23+
/** @var float */
24+
public $ctr;
25+
/** @var string */
26+
public $source;
27+
/** @var DateTime */
28+
public $date;
29+
30+
public function __construct()
31+
{
32+
if (isset($this->date) && is_string($this->date)) {
33+
$this->date = DateTime::createFromFormat('Y-m-d', $this->date);
34+
}
35+
}
1136
}

‎src/Ad/V1__Ad.sql

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
create table ad
22
(
3-
id uuid not null primary key,
4-
name varchar not null,
5-
timestamp timestamp not null default current_timestamp
6-
);
3+
id uuid not null primary key,
4+
name varchar not null,
5+
cost float not null default 0,
6+
impressions int not null default 0,
7+
clicks int not null default 0,
8+
cpm float not null default 0,
9+
cpc float not null default 0,
10+
ctr float not null default 0,
11+
source varchar not null,
12+
date date not null,
13+
timestamp timestamp not null default current_timestamp
14+
);

‎src/ETL/GoogleAnalytics.php renamed to ‎src/ETL/AdWords.php

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,23 @@
44

55
use AdsWarehouse\Ad\Ad;
66
use AdsWarehouse\Warehouse\Warehouse;
7+
use DateTime;
8+
use Exception;
79
use Google_Service_AnalyticsReporting;
810
use Google_Service_AnalyticsReporting_DateRange;
11+
use Google_Service_AnalyticsReporting_Dimension as Dimension;
912
use Google_Service_AnalyticsReporting_GetReportsRequest;
1013
use Google_Service_AnalyticsReporting_GetReportsResponse;
1114
use Google_Service_AnalyticsReporting_Metric as Metric;
1215
use Google_Service_AnalyticsReporting_Report;
1316
use Google_Service_AnalyticsReporting_ReportRequest;
17+
use Google_Service_AnalyticsReporting_ReportRow;
18+
use Ramsey\Uuid\Uuid;
1419

15-
class GoogleAnalytics extends ETL
20+
class AdWords extends ETL
1621
{
22+
const SOURCE = 'AdWords';
23+
1724
/** @var Google_Service_AnalyticsReporting */
1825
private $analytics;
1926
/** @var string */
@@ -36,10 +43,14 @@ protected function extract(): Google_Service_AnalyticsReporting_GetReportsRespon
3643
$date_range->setStartDate('yesterday');
3744
$date_range->setEndDate('yesterday');
3845

46+
$campaignId = new Dimension();
47+
$campaignId->setName('ga:campaign');
48+
3949
$request = new Google_Service_AnalyticsReporting_ReportRequest();
4050
$request->setViewId($this->viewId);
4151
$request->setDateRanges($date_range);
4252
$request->setMetrics($this->getMetrics());
53+
$request->setDimensions([$campaignId]);
4354

4455
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
4556
$body->setReportRequests([$request]);
@@ -48,11 +59,41 @@ protected function extract(): Google_Service_AnalyticsReporting_GetReportsRespon
4859
}
4960

5061
/**
51-
* @return Metric[]
62+
* @param Google_Service_AnalyticsReporting_GetReportsResponse $data
63+
* @return Ad[]
64+
* @throws Exception
5265
*/
53-
private function getMetrics(): array
66+
protected function transform($data): array
5467
{
55-
return array_map([$this, 'newMetricFromExpression'], $this->getExpressions());
68+
/** @var Google_Service_AnalyticsReporting_Report $report */
69+
$report = $data->getReports()[0];
70+
71+
$data = $report->getData();
72+
$ads = [];
73+
74+
/** @var Google_Service_AnalyticsReporting_ReportRow $row */
75+
foreach ($data->getRows() as $row) {
76+
/** @var string $campaign */
77+
$campaign = $row->getDimensions()[0];
78+
$metrics = $row->getMetrics()[0];
79+
$values = $metrics->getValues();
80+
81+
$ad = new Ad();
82+
$ad->id = Uuid::uuid4();
83+
$ad->name = $campaign;
84+
$ad->impressions = $values[3];
85+
$ad->clicks = $values[4];
86+
$ad->cost = $values[5];
87+
$ad->cpm = $values[6];
88+
$ad->cpc = $values[7];
89+
$ad->ctr = $values[8];
90+
$ad->source = $this->getSource();
91+
$ad->date = (new DateTime())->modify('-1 day');
92+
93+
$ads[] = $ad;
94+
}
95+
96+
return $ads;
5697
}
5798

5899
/**
@@ -64,24 +105,21 @@ private function getExpressions(): array
64105
'ga:users',
65106
'ga:newUsers',
66107
'ga:sessions',
108+
'ga:impressions',
109+
'ga:adClicks',
110+
'ga:adCost',
111+
'ga:CPM',
112+
'ga:CPC',
113+
'ga:CTR',
67114
];
68115
}
69116

70117
/**
71-
* @param Google_Service_AnalyticsReporting_GetReportsResponse $data
72-
* @return Ad
118+
* @return Metric[]
73119
*/
74-
protected function transform($data): Ad
120+
private function getMetrics(): array
75121
{
76-
/** @var Google_Service_AnalyticsReporting_Report $report */
77-
$report = $data->getReports()[0];
78-
79-
$data = $report->getData();
80-
81-
var_dump($data->getTotals());
82-
83-
$ad = new Ad();
84-
return $ad;
122+
return array_map([$this, 'newMetricFromExpression'], $this->getExpressions());
85123
}
86124

87125
private function newMetricFromExpression(string $expression): Metric
@@ -91,4 +129,9 @@ private function newMetricFromExpression(string $expression): Metric
91129

92130
return $metric;
93131
}
132+
133+
protected function getSource(): string
134+
{
135+
return self::SOURCE;
136+
}
94137
}

‎src/ETL/ETL.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use AdsWarehouse\Ad\Ad;
66
use AdsWarehouse\Warehouse\Warehouse;
7+
use DateTime;
78

89
abstract class ETL
910
{
@@ -17,10 +18,17 @@ public function __construct(Warehouse $warehouse)
1718

1819
public function load()
1920
{
21+
$this->warehouse->drop($this->getSource(), (new DateTime())->modify('-1 day'));
2022
$this->warehouse->store($this->transform($this->extract()));
2123
}
2224

23-
abstract protected function transform($data): Ad;
25+
/**
26+
* @param $data
27+
* @return Ad[]
28+
*/
29+
abstract protected function transform($data): array;
2430

2531
abstract protected function extract();
32+
33+
abstract protected function getSource(): string;
2634
}

‎src/Warehouse/Pdo.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace AdsWarehouse\Warehouse;
44

55
use AdsWarehouse\Ad\Ad;
6+
use DateTime;
67

78
class Pdo implements Warehouse
89
{
@@ -14,15 +15,30 @@ public function __construct(\PDO $pdo)
1415
$this->pdo = $pdo;
1516
}
1617

17-
public function store(Ad $ad): bool
18+
/**
19+
* @param Ad[] $ads
20+
* @return bool
21+
*/
22+
public function store(array $ads): void
1823
{
1924
$stmt = $this->pdo->prepare(
20-
'insert into ad (id, name) values (:id, :name)'
25+
'insert into ad (id, name, cost, impressions, clicks, cpm, cpc, ctr, source, date) values (:id, :name, :cost, :impressions, :clicks, :cpm, :cpc, :ctr, :source, :date)'
2126
);
22-
$stmt->bindValue('id', $ad->id);
23-
$stmt->bindValue('name', $ad->name);
2427

25-
return $stmt->execute();
28+
foreach ($ads as $ad) {
29+
$stmt->bindValue('id', $ad->id);
30+
$stmt->bindValue('name', $ad->name);
31+
$stmt->bindValue('cost', $ad->cost);
32+
$stmt->bindValue('impressions', $ad->impressions, \PDO::PARAM_INT);
33+
$stmt->bindValue('clicks', $ad->clicks, \PDO::PARAM_INT);
34+
$stmt->bindValue('cpm', $ad->cpm);
35+
$stmt->bindValue('cpc', $ad->cpc);
36+
$stmt->bindValue('ctr', $ad->ctr);
37+
$stmt->bindValue('source', $ad->source);
38+
$stmt->bindValue('date', $ad->date->format('Y-m-d'));
39+
40+
$stmt->execute();
41+
}
2642
}
2743

2844
/**
@@ -33,4 +49,9 @@ public function items(): array
3349
return $this->pdo->query('select * from ad order by timestamp desc')
3450
->fetchAll(\PDO::FETCH_CLASS, Ad::class);
3551
}
52+
53+
public function drop(string $source, DateTime $date): void
54+
{
55+
$this->pdo->prepare('delete from ad where source = ? and date = ?')->execute([$source, $date->format('Y-m-d')]);
56+
}
3657
}

‎src/Warehouse/Warehouse.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
namespace AdsWarehouse\Warehouse;
44

55
use AdsWarehouse\Ad\Ad;
6+
use DateTime;
67

78
interface Warehouse
89
{
9-
public function store(Ad $ad): bool;
10+
/**
11+
* @param Ad[] $ads
12+
*/
13+
public function store(array $ads): void;
1014

1115
/**
1216
* @return Ad[]
1317
*/
1418
public function items(): array;
19+
20+
public function drop(string $source, DateTime $date): void;
1521
}

0 commit comments

Comments
 (0)
Please sign in to comment.