MixpanelPeople.php
10.8 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
<?php
require_once(dirname(__FILE__) . "/MixpanelBaseProducer.php");
/**
* Provides an API to create/update profiles on Mixpanel
*/
class Producers_MixpanelPeople extends Producers_MixpanelBaseProducer {
/**
* Internal method to prepare a message given the message data
* @param $distinct_id
* @param $operation
* @param $value
* @param null $ip
* @param boolean $ignore_time If the $ignore_time property is true, Mixpanel will not automatically update the "Last Seen" property of the profile. Otherwise, Mixpanel will add a "Last Seen" property associated with the current time
* @param boolean $ignore_alias If the $ignore_alias property is true, an alias look up will not be performed after ingestion. Otherwise, a lookup for the distinct ID will be performed, and replaced if a match is found
* @return array
*/
private function _constructPayload($distinct_id, $operation, $value, $ip = null, $ignore_time = false, $ignore_alias = false) {
$payload = array(
'$token' => $this->_token,
'$distinct_id' => $distinct_id,
'$time' => microtime(true),
$operation => $value
);
if ($ip !== null) $payload['$ip'] = $ip;
if ($ignore_time === true) $payload['$ignore_time'] = true;
if ($ignore_alias === true) $payload['$ignore_alias'] = true;
return $payload;
}
/**
* Set properties on a user record. If the profile does not exist, it creates it with these properties.
* If it does exist, it sets the properties to these values, overwriting existing values.
* @param string|int $distinct_id the distinct_id or alias of a user
* @param array $props associative array of properties to set on the profile
* @param string|null $ip the ip address of the client (used for geo-location)
* @param boolean $ignore_time If the $ignore_time property is true, Mixpanel will not automatically update the "Last Seen" property of the profile. Otherwise, Mixpanel will add a "Last Seen" property associated with the current time
* @param boolean $ignore_alias If the $ignore_alias property is true, an alias look up will not be performed after ingestion. Otherwise, a lookup for the distinct ID will be performed, and replaced if a match is found
*/
public function set($distinct_id, $props, $ip = null, $ignore_time = false, $ignore_alias = false) {
$payload = $this->_constructPayload($distinct_id, '$set', $props, $ip, $ignore_time, $ignore_alias);
$this->enqueue($payload);
}
/**
* Set properties on a user record. If the profile does not exist, it creates it with these properties.
* If it does exist, it sets the properties to these values but WILL NOT overwrite existing values.
* @param string|int $distinct_id the distinct_id or alias of a user
* @param array $props associative array of properties to set on the profile
* @param string|null $ip the ip address of the client (used for geo-location)
* @param boolean $ignore_time If the $ignore_time property is true, Mixpanel will not automatically update the "Last Seen" property of the profile. Otherwise, Mixpanel will add a "Last Seen" property associated with the current time
* @param boolean $ignore_alias If the $ignore_alias property is true, an alias look up will not be performed after ingestion. Otherwise, a lookup for the distinct ID will be performed, and replaced if a match is found
*/
public function setOnce($distinct_id, $props, $ip = null, $ignore_time = false, $ignore_alias = false) {
$payload = $this->_constructPayload($distinct_id, '$set_once', $props, $ip, $ignore_time, $ignore_alias);
$this->enqueue($payload);
}
/**
* Unset properties on a user record. If the profile does not exist, it creates it with no properties.
* If it does exist, it unsets these properties. NOTE: In other libraries we use 'unset' which is
* a reserved word in PHP.
* @param string|int $distinct_id the distinct_id or alias of a user
* @param array $props associative array of properties to unset on the profile
* @param string|null $ip the ip address of the client (used for geo-location)
* @param boolean $ignore_time If the $ignore_time property is true, Mixpanel will not automatically update the "Last Seen" property of the profile. Otherwise, Mixpanel will add a "Last Seen" property associated with the current time
* @param boolean $ignore_alias If the $ignore_alias property is true, an alias look up will not be performed after ingestion. Otherwise, a lookup for the distinct ID will be performed, and replaced if a match is found
*/
public function remove($distinct_id, $props, $ip = null, $ignore_time = false, $ignore_alias = false) {
$payload = $this->_constructPayload($distinct_id, '$unset', $props, $ip, $ignore_time, $ignore_alias);
$this->enqueue($payload);
}
/**
* Increments the value of a property on a user record. If the profile does not exist, it creates it and sets the
* property to the increment value.
* @param string|int $distinct_id the distinct_id or alias of a user
* @param $prop string the property to increment
* @param int $val the amount to increment the property by
* @param string|null $ip the ip address of the client (used for geo-location)
* @param boolean $ignore_time If the $ignore_time property is true, Mixpanel will not automatically update the "Last Seen" property of the profile. Otherwise, Mixpanel will add a "Last Seen" property associated with the current time
* @param boolean $ignore_alias If the $ignore_alias property is true, an alias look up will not be performed after ingestion. Otherwise, a lookup for the distinct ID will be performed, and replaced if a match is found
*/
public function increment($distinct_id, $prop, $val, $ip = null, $ignore_time = false, $ignore_alias = false) {
$payload = $this->_constructPayload($distinct_id, '$add', array("$prop" => $val), $ip, $ignore_time, $ignore_alias);
$this->enqueue($payload);
}
/**
* Adds $val to a list located at $prop. If the property does not exist, it will be created. If $val is a string
* and the list is empty or does not exist, a new list with one value will be created.
* @param string|int $distinct_id the distinct_id or alias of a user
* @param string $prop the property that holds the list
* @param string|array $val items to add to the list
* @param string|null $ip the ip address of the client (used for geo-location)
* @param boolean $ignore_time If the $ignore_time property is true, Mixpanel will not automatically update the "Last Seen" property of the profile. Otherwise, Mixpanel will add a "Last Seen" property associated with the current time
* @param boolean $ignore_alias If the $ignore_alias property is true, an alias look up will not be performed after ingestion. Otherwise, a lookup for the distinct ID will be performed, and replaced if a match is found
*/
public function append($distinct_id, $prop, $val, $ip = null, $ignore_time = false, $ignore_alias = false) {
$operation = gettype($val) == "array" ? '$union' : '$append';
$payload = $this->_constructPayload($distinct_id, $operation, array("$prop" => $val), $ip, $ignore_time, $ignore_alias);
$this->enqueue($payload);
}
/**
* Adds a transaction to the user's profile for revenue tracking
* @param string|int $distinct_id the distinct_id or alias of a user
* @param string $amount the transaction amount e.g. "20.50"
* @param null $timestamp the timestamp of when the transaction occurred (default to current timestamp)
* @param string|null $ip the ip address of the client (used for geo-location)
* @param boolean $ignore_time If the $ignore_time property is true, Mixpanel will not automatically update the "Last Seen" property of the profile. Otherwise, Mixpanel will add a "Last Seen" property associated with the current time
* @param boolean $ignore_alias If the $ignore_alias property is true, an alias look up will not be performed after ingestion. Otherwise, a lookup for the distinct ID will be performed, and replaced if a match is found
*/
public function trackCharge($distinct_id, $amount, $timestamp = null, $ip = null, $ignore_time = false, $ignore_alias = false) {
$timestamp = $timestamp == null ? time() : $timestamp;
$date_iso = date("c", $timestamp);
$transaction = array(
'$time' => $date_iso,
'$amount' => $amount
);
$val = array('$transactions' => $transaction);
$payload = $this->_constructPayload($distinct_id, '$append', $val, $ip, $ignore_time, $ignore_alias);
$this->enqueue($payload);
}
/**
* Clear all transactions stored on a user's profile
* @param string|int $distinct_id the distinct_id or alias of a user
* @param string|null $ip the ip address of the client (used for geo-location)
* @param boolean $ignore_time If the $ignore_time property is true, Mixpanel will not automatically update the "Last Seen" property of the profile. Otherwise, Mixpanel will add a "Last Seen" property associated with the current time
* @param boolean $ignore_alias If the $ignore_alias property is true, an alias look up will not be performed after ingestion. Otherwise, a lookup for the distinct ID will be performed, and replaced if a match is found
*/
public function clearCharges($distinct_id, $ip = null, $ignore_time = false, $ignore_alias = false) {
$payload = $this->_constructPayload($distinct_id, '$set', array('$transactions' => array()), $ip, $ignore_time, $ignore_alias);
$this->enqueue($payload);
}
/**
* Delete this profile from Mixpanel
* @param string|int $distinct_id the distinct_id or alias of a user
* @param string|null $ip the ip address of the client (used for geo-location)
* @param boolean $ignore_time If the $ignore_time property is true, Mixpanel will not automatically update the "Last Seen" property of the profile. Otherwise, Mixpanel will add a "Last Seen" property associated with the current time
* @param boolean $ignore_alias If the $ignore_alias property is true, an alias look up will not be performed after ingestion. Otherwise, a lookup for the distinct ID will be performed, and replaced if a match is found
*/
public function deleteUser($distinct_id, $ip = null, $ignore_time = false, $ignore_alias = false) {
$payload = $this->_constructPayload($distinct_id, '$delete', "", $ip, $ignore_time, $ignore_alias);
$this->enqueue($payload);
}
/**
* Returns the "engage" endpoint
* @return string
*/
function _getEndpoint() {
return $this->_options['people_endpoint'];
}
}