1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TestMonitor\ActiveCampaign\Actions; |
4
|
|
|
|
5
|
|
|
use TestMonitor\ActiveCampaign\Resources\Event; |
6
|
|
|
|
7
|
|
|
trait ManagesEvents |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* This value is unique to ActiveCampaign account and can be found named "Event Key" |
11
|
|
|
* on Settings > Tracking > Event Tracking inside ActiveCampaign account. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
public $eventKey; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This value is unique to ActiveCampaign account and can be found named "actid" |
19
|
|
|
* on Settings > Tracking > Event Tracking API. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
public $actid; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Creates a new event (name only) |
27
|
|
|
* |
28
|
|
|
* @param string $name |
29
|
|
|
* |
30
|
|
|
* @return Event |
31
|
|
|
*/ |
32
|
|
|
public function createEvent($name) |
33
|
|
|
{ |
34
|
|
|
$data = [ |
35
|
|
|
'name' => $name, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
$event = $this->post('eventTrackingEvents', ['json' => ['eventTrackingEvent' => $data]]); |
|
|
|
|
39
|
|
|
|
40
|
|
|
return new Event($event['eventTrackingEvent']); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* List all events |
45
|
|
|
* |
46
|
|
|
* @return Event[] |
47
|
|
|
*/ |
48
|
|
|
public function events() |
49
|
|
|
{ |
50
|
|
|
return $this->transformCollection( |
|
|
|
|
51
|
|
|
$this->get('eventTrackingEvents'), |
|
|
|
|
52
|
|
|
Event::class, |
53
|
|
|
'eventTrackingEvents' |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* List name of all events |
59
|
|
|
* |
60
|
|
|
* @return Event[] |
61
|
|
|
*/ |
62
|
|
|
public function eventNames() |
63
|
|
|
{ |
64
|
|
|
$events = $this->get('eventTrackingEvents'); |
|
|
|
|
65
|
|
|
|
66
|
|
|
return array_map(function ($data) { return $data['name']; }, |
67
|
|
|
$events['eventTrackingEvents'] ?? $events); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Removes an existing event tracking event (name only) |
72
|
|
|
* |
73
|
|
|
* @param $name |
74
|
|
|
* |
75
|
|
|
* @throws \TestMonitor\ActiveCampaign\Exceptions\NotFoundException |
76
|
|
|
*/ |
77
|
|
|
public function deleteEvent($name) |
78
|
|
|
{ |
79
|
|
|
$this->delete('eventTrackingEvents/' . $name); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Tracks an event by name |
84
|
|
|
* |
85
|
|
|
* @param string $name name of the event to track |
86
|
|
|
* @param string $email email address of the contact to track this event for, optional |
87
|
|
|
* @param array $eventData a value to store for the event, optional |
88
|
|
|
* |
89
|
|
|
* @return bool TRUE on success, FALSE otherwise |
90
|
|
|
* @throws \TestMonitor\ActiveCampaign\Exceptions\FailedActionException |
91
|
|
|
*/ |
92
|
|
|
public function trackEvent($name, $email = '', $eventData = []) |
93
|
|
|
{ |
94
|
|
|
$curl = curl_init(); |
95
|
|
|
curl_setopt($curl, CURLOPT_URL, 'https://trackcmp.net/event'); |
96
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
97
|
|
|
curl_setopt($curl, CURLOPT_POST, true); |
98
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, [ |
99
|
|
|
'actid' => $this->actid, |
100
|
|
|
'key' => $this->eventKey, |
101
|
|
|
'event' => $name, |
102
|
|
|
'eventdata' => json_encode($eventData), |
103
|
|
|
] + ($email ? ['visit' => json_encode(['email' => $email])] : [])); |
104
|
|
|
|
105
|
|
|
$result = curl_exec($curl); |
106
|
|
|
if ($result !== false) |
107
|
|
|
{ |
108
|
|
|
curl_close($curl); |
109
|
|
|
$result = json_decode($result); |
110
|
|
|
|
111
|
|
|
if (isset($result->success) && $result->success) return true; |
112
|
|
|
|
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
else |
116
|
|
|
{ |
117
|
|
|
throw new \TestMonitor\ActiveCampaign\Exceptions\FailedActionException(curl_error($curl)); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.