-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkClickToPercentage.php
More file actions
256 lines (223 loc) · 6.07 KB
/
Copy pathLinkClickToPercentage.php
File metadata and controls
256 lines (223 loc) · 6.07 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
require 'ImageStyle.php';
/**
* Class LinkClickToPercentage
*/
class LinkClickToPercentage
{
const LOWER_LEFT_XPOS = 0;
const LOWER_RIGHT_XPOS = 2;
const UPPER_LEFT_YPOS = 7;
const LOWER_LEFT_YPOS = 1;
const STYLES_CONFIG_FILE = 'styles/styles.json';
const TRACKING_CONFIG_FILE = 'config/tracking.json';
const COUNT = 100;
/**
* @var array
*/
private $result;
/**
* @var string
*/
private $apiKey;
/**
* @var string
*/
private $linkId;
/**
* @var string
*/
private $emailName;
/**
* @var object
*/
private $emailDetails;
/**
* @var array
*/
private $stylesConfigs;
/**
* LinkClickToPercentage constructor.
*
* @param $apiKey
* @param $emailId
* @param $style
*/
public function __construct($apiKey)
{
$this->apiKey = $apiKey; // api key linked to mailchimp account
$this->linkId = isset($_GET['linkId']) ? $_GET['linkId'] : ''; // unique ident used to track each link
$this->emailName = isset($_GET['emailName']) ? $_GET['emailName'] : ''; // name of email campaign in tracking.json
$this->emailDetails = $this->getEmailDetails($this->emailName); // email config from tracking.json
$this->stylesConfigs = json_decode(file_get_contents(__DIR__ . '/' . self::STYLES_CONFIG_FILE), true); // generic style configs
if ($this->apiKey == "API_KEY") {
echo "Please enter a valid API Key."; exit;
}
$this->requestData();
$this->createImage();
}
/**
* requestData
* curl request to get data
* from mailchimp
* @return array
*/
public function requestData()
{
$curl = curl_init();
curl_setopt(
$curl,
CURLOPT_URL,
$this->buildRequestUrl()
);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Basic ' . base64_encode('user:' . $this->apiKey),
]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
return $this->result = json_decode(curl_exec($curl), true);
}
/**
* getEmailDetails
* retrieve email details from
* tracking.json
* @param string $ident [email name]
* @return object
*/
private function getEmailDetails($ident)
{
$config = json_decode(file_get_contents(__DIR__ . '/' . self::TRACKING_CONFIG_FILE), true);
return $config[$ident];
}
/**
* getStyleFromConfig
* create the style from the style
* specificed in the config json
* @param string $style
* @return object
*/
private function getStyleFromConfig()
{
return new ImageStyle(
$this->stylesConfigs[$this->emailDetails['style']]['width'],
$this->stylesConfigs[$this->emailDetails['style']]['height'],
$this->stylesConfigs[$this->emailDetails['style']]['backgroundColor'],
$this->stylesConfigs[$this->emailDetails['style']]['textSize'],
$this->stylesConfigs[$this->emailDetails['style']]['textAngle'],
$this->stylesConfigs[$this->emailDetails['style']]['fontFile'],
$this->stylesConfigs[$this->emailDetails['style']]['textColor']
);
}
/**
* createImage
* @return object
*/
private function createImage()
{
$this->totalClicks = $this->getLinksStatistics($this->result['urls_clicked']);
$percentage = $this->getClickPercentage($this->result['urls_clicked'], $this->linkId); // get the percentage of links for url
$style = $this->getStyleFromConfig();
// create base image
$img = imagecreatetruecolor($style->width, $style->height);
imagefilledrectangle(
$img,
0,
0,
$style->width,
$style->height,
imagecolorallocate($img,
$style->backgroundColor['r'],
$style->backgroundColor['g'],
$style->backgroundColor['b']
)
);
// get text dimensions for centering
$boundingBox = imagettfbbox(
$style->textSize,
$style->textAngle,
$style->fontFile,
$percentage
);
$textWidth = abs($boundingBox[self::LOWER_RIGHT_XPOS] - $boundingBox[self::LOWER_LEFT_XPOS]);
$textHeight = abs($boundingBox[self::LOWER_LEFT_YPOS] - $boundingBox[self::UPPER_LEFT_YPOS]);
$textX = round(($style->width / 2) - ($textWidth / 2));
$textY = (round(($style->height / 2) + ($textHeight / 2))) - 2; // move it a couple pixels above centre
// apply text to image
imagettftext($img,
$style->textSize,
$style->textAngle,
$textX,
$textY,
imagecolorallocate($img,
$style->textColor['r'],
$style->textColor['g'],
$style->textColor['b']
),
$style->fontFile,
$percentage
);
$this->outputImage($img);
}
/**
* outputImage
* output the image
* @param $img
* @param $type
*/
private function outputImage($img, $type = 'png')
{
header('Content-Type:image/' . $type);
header("Pragma-directive: no-cache");
header("Cache-directive: no-cache");
header("Cache-control: no-cache");
header("Pragma: no-cache");
header("Expires: 0");
imagepng($img);
}
/**
* getClickPercentage
* looks for a str match in the data
* to get the percentage of times
* the link has been clicked
* @param $array
* @param $str
* @return string
*/
private function getClickPercentage($array, $str)
{
foreach ($array as $key => $val) {
if (strpos($val['url'], 'linkID=' . $str) !== false) {
return round(($val['unique_clicks'] / $this->totalClicks) * 100) . '%';
}
}
return null;
}
/**
* getLinksStatistics
* get the total number of unique clicks
* on tracked links
* @param $array
* @return $totalClicks
*/
private function getLinksStatistics($array)
{
$totalClicks = 0;
foreach ($array as $key => $val) {
if (strpos($val['url'], 'linkID') !== false) {
$totalClicks = $totalClicks + $val['unique_clicks'];
}
}
return $totalClicks;
}
/**
* @return string
*/
private function buildRequestUrl()
{
return str_replace(
'{{email_id}}',
$this->emailDetails['emailId'],
'https://us4.api.mailchimp.com/3.0/reports/{{email_id}}/click-details?count=' . self::COUNT
);
}
}