-
Notifications
You must be signed in to change notification settings - Fork 0
/
partners.cookie.php
137 lines (107 loc) · 4.14 KB
/
partners.cookie.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
<?php
/*
Make sure this code is included at any page of your site before any output is started.
The good way is to place it in the beginning of your index.php
*/
$postbacksSettingsByPartner = [
'cityads' => [ // contact cityads's integration team to configure this part
'pixel-url' => 'https://hskwq.com/service/newtrack/{orderId}/ct/{targetCode}/c/{campaignId}?click_id={clickId}',
'postback-url' => 'https://cityads.ru/service/postback?order_id={orderId}&status={status}&click_id={clickId}',
],
/*'anotherPartner' => [
'pixel-url' => 'anotherUrl',
'placeholders' => [
// contact partner's integration team to configure this part
]
]*/
];
$cpaCounter = new CPACounter($postbacksSettingsByPartner);
$cpaCounter->setPartnersCookies();
class CPACounter {
private static $postbacksSettingsByPartner;
public function __construct($postbacksSettingsByPartner = null) {
if (isset($postbacksSettingsByPartner) && !isset(self::$postbacksSettingsByPartner)) {
self::$postbacksSettingsByPartner = $postbacksSettingsByPartner;
} else {
trigger_error('CPACounter should be firstly instanced with postbacksSettingsByPartner argument, then it musql be used with no argument.', E_ERROR);
}
}
public function setPartnersCookies($expirationDays = 120) {
if (!isset($_GET['utm_source'])) {
return;
}
foreach (array_keys(self::$postbacksSettingsByPartner) as $partnerName) {
if ($_GET['utm_source'] === $partnerName) {
ob_start();
setcookie($partnerName . 'ClickId', isset($_GET['utm_source']) ? $_GET['utm_source'] : '', time() + 86400 * $expirationDays, '/');
ob_end_flush();
}
}
}
public function getPixels($orderId, $targetCode, $partnerNames = ['cityads']) {
if (!isset($partnerNames)) {
$partnerNames = array_keys(self::$postbacksSettingsByPartner);
}
$urls = [];
foreach ($partnerNames as $partnerName) {
if (!isset($_COOKIE[$partnerName . 'ClickId'])) {
continue;
}
$placeholders = [
'{orderId}' => urlencode($orderId),
'{targetCode}' => $targetCode,
'{campaignId}' => $campaignId,
'{clickId}' => $_COOKIE[$partnerName . 'ClickId'],
];
$urls[] = '<img src="' . str_replace(
array_keys($placeholders),
$placeholders,
self::$postbacksSettingsByPartner[$partnerName]['pixel-url']
. '" style="display:none">'
);
}
return $urls ?: null;
}
public function sendPostback($orderId, $status, $partnerNames = ['cityads']) {
if (!isset($partnerNames)) {
$partnerNames = array_keys(self::$postbacksSettingsByPartner);
}
foreach ($partnerNames as $partnerName) {
if (!isset($_COOKIE[$partnerName . 'ClickId'])) {
continue;
}
$placeholders = [
'{orderId}' => urlencode($orderId),
'{status}' => $status,
'{clickId}' => $_COOKIE[$partnerName . 'ClickId'],
];
$url = str_replace(
array_keys($placeholders),
$placeholders,
self::$postbacksSettingsByPartner[$partnerName]['postback-url']
);
var_dump($url);
if (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_close($ch);
} else {
file_get_contents($url) || `wget "$url"`;
}
}
return null;
}
}
/**********************************************************************************/
/*
Place this code after the order is successfully done. Postback will be sent to the CPA partner whose cookie were installed before;
*/
$cpaCounter = new CPACounter();
$cpaCounter->sendPostback($orderId, 'open'); // $orderId is an ID of the order in your system. Status is 'open' when order is just created. Contact integration team to talk about approving commission.
/*
Or place this code after the order is successfully done
*/
$cpaCounter = new CPACounter();
$pixels = $cpaCounter->getPixels($orderId, $targetCode); // $orderId is an ID of the order in your system. Contact integration team to talk about $targetCode
// Next you should give these $pixel to your web page and draw them. It very depends on your framework/CMS
var_dump($_COOKIE, $_GET, $pixels);