-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dashbot.php
165 lines (145 loc) · 4.25 KB
/
Dashbot.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
<?php
namespace Apix\Log;
use Apix\Log\Emitter\EmitterInterface as LogEmitter;
use Psr\Log\InvalidArgumentException;
/**
* Dashbot logger for Apix Log.
*
* @see https://www.dashbot.io/sdk/generic
*/
class Dashbot extends AbstractTracker
{
const TRACKER_URL =
'https://tracker.dashbot.io/track?platform=%s&v=%s&type=%s&apiKey=%s';
const TRANSPORTER_CMD =
'curl -X POST -d %1$s \'%2$s\' -H \'Content-Type: application/json\'';
// const DEFAULT_PARAMS = array(
private $DEFAULT_PARAMS = array(
'platform' => 'generic', // Either generic, facebook, slack, kik
'v' => '0.7.4-rest', // API Version
'type' => null, // Hit type (required)
'apiKey' => null, // API key (required)
);
/**
* Constructor.
*
* @param
*/
public function __construct(
$mixed, LogEmitter $emitter = null, LogFormatter $formatter = null
) {
$this->setEmitter(
$emitter ? $emitter : new Emitter\Async(self::TRANSPORTER_CMD),
$formatter ? $formatter : new LogFormatter\Json()
);
$this->emitter->setParams($this->DEFAULT_PARAMS);
if (is_array($mixed) && isset($mixed['apiKey'])) {
$this->emitter->addParams($mixed);
} elseif (is_string($mixed)) {
$this->emitter->setParam('apiKey', $mixed);
} else {
throw new InvalidArgumentException(sprintf(
'%s expects `apiKey` to be set, got: %s.',
__CLASS__, json_encode($mixed)
));
}
}
/**
* Sets the platform (format).
*
* @see https://www.dashbot.io/sdk/template
*
* @param string $platform Either 'generic', 'facebook', 'slack', 'kik'
*
* @return self
*/
public function setPlatform($platform)
{
$this->emitter->setParam('platform', $platform);
return $this;
}
/**
* Sets a global tag (used to combined metrics).
*
* @see https://www.dashbot.io/sdk/template
*
* @param array $entries
* @param string $local_tag
*
* @return self
*/
public function setGlobalTag($global_tag = null)
{
$this->emitter->setParam('dashbotTemplateId', $global_tag);
return $this;
}
/**
* Rewrite the dashbot template Id.
*
* @see https://www.dashbot.io/sdk/template
*
* @param array $entries
* @param array $params
*
* @return array
*/
public function rewriteTemplateId(array $entries, array $params)
{
if (isset($entries['json']) && $params['platform'] == 'facebook') {
if ($tpl_id = // get from entries (local) then params (global).
$entries['dashbotTemplateId'] ?: $params['dashbotTemplateId'] ?: false
) {
$entries['json']['dashbotTemplateId'] = $tpl_id;
// remove from `entries` (local) to avoid duplicate.
unset($entries['dashbotTemplateId']);
}
}
return $entries;
}
/**
* Returns the named tracking dataset.
*
* @param string $type
* @param array $entries
* @param string $local_tag
*
* @return array
*/
public function get($type, array $entries, $local_tag = null)
{
if ($local_tag) {
$entries['dashbotTemplateId'] = $local_tag;
}
$entries = $this->rewriteTemplateId(
$entries, $this->emitter->getParams()
);
$this->emitter->setParam('type', $type);
$this->emitter->setUrl(self::TRACKER_URL);
$this->data= [ $type, $entries ];
return (array) $entries;
}
/**
* Returns an incoming tracking dataset.
*
* @param array $entries
* @param string $local_tag
*
* @return array
*/
public function incoming(array $entries, $local_tag = null)
{
return $this->get(__FUNCTION__, $entries, $local_tag);
}
/**
* Returns an outgoing tracking dataset.
*
* @param array $entries
* @param string $local_tag
*
* @return array
*/
public function outgoing(array $entries, $local_tag = null)
{
return $this->get(__FUNCTION__, $entries, $local_tag);
}
}