-
Notifications
You must be signed in to change notification settings - Fork 0
/
listSegmentMaker.php
282 lines (238 loc) · 7.85 KB
/
listSegmentMaker.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
ini_set('include_path','..'.DIRECTORY_SEPARATOR.'library'.DIRECTORY_SEPARATOR.PATH_SEPARATOR.ini_get('include_path'));
/*
* listSegmenterMaker
*
* This script looks at your last mailchimp campaigns. if the campaign name (not
* subject line) has the query in it then it will gather together all of the
* addresses of people who have opened that campaign. It will do this for every
* campaign that matches the criteria. It will then either create a new segment
* or update an existing segment.
*/
/*
* The MailChimp API wrapper
* http://apidocs.mailchimp.com/api/downloads/
*/
require_once 'MCAPI.php';
/*
* Initalize needed variables
*/
$campaignIds = array();
$finalListofEmails = array();
$segmentId = null;
$segmentTitle = null;
/*
* Fetch and validate the cli options
*
* CLI Options
* -l = listId. Required
* -s = segmentId. If it does not exist then create it
* -n = Segment name. Require for new segments, optional for existing segments.
* -q = The query to look for in the campaign name.
* -f = fetch list of segments and IDs for a given list
* -g = fetch list of lists
* -v = verbose mode. shows what is going on.
* -t = test mode. doesn't actually do anything.
*
*/
$options = getopt("a:l:q:s::n::fvtg");
$apiKey = isset($options['a'])?$options['a']:null;
$listId = isset($options['l'])?$options['l']:null;
$segmentId = isset($options['s'])?$options['s']:null;
$segmentTitle = isset($options['n'])?$options['n']:null;
$fetchListMode = isset($options['g'])?true:false;
$fetchSegmentMode = isset($options['f'])?true:false;
$verboseMode = isset($options['v'])?true:false;
$testMode = isset($options['t'])?true:false;
if ($verboseMode) {
outputHeader();
}
if (is_null($apiKey)) {
outputError("No mailchimp api key provided.\n",$verboseMode,$argv,$options);
die(-1);
}
if (!$fetchListMode and is_null($listId)) {
outputError("No list id provided.\n",$verboseMode,$argv,$options);
die(-2);
}
/*
* If we are not asking for a list of lists or a list of segments, and a
* segment id or title was not provided, bail.
*/
if ((!$fetchListMode and !$fetchListMode) and
(is_null($segmentId) && is_null($segmentTitle))) {
outputError("You must provide either a segment id or the title for a new segment.\n",$verboseMode,$argv,$options);
die(-3);
}
/*
* If verbose mode, motify the user of the options set.
*/
if ($verboseMode) {
echo "Options\n";
echo "=======\n";
echo "MailChimp API Key : {$apiKey}\n";
echo "List Id : {$listId}\n";
echo "Segment Id : {$segmentId}\n";
echo "Segment Title : " . (is_null($segmentId)?$segmentTitle:"IGNORED") . "\n";
echo "Create New Segment : " . (is_null($segmentId)?"true":"false") . "\n";
echo "Fetch list mode : " . ($fetchListMode?"true":"false") . "\n";
echo "Fetch segment mode : " . ($fetchSegmentMode?"true":"false") . "\n";
echo "Test Mode : " . ($testMode?"true":"false") . "\n\n";
}
/*
* Connect to MailChimp
*/
$api = new MCAPI($apiKey);
/*
* If the user has asked for a list of list then just show that
* and bail. Fetch Mode assumes verbose mode.
*/
if ($fetchListMode) {
$lists = $api->lists();
echo "List of Lists\n";
echo "=============\n";
if (is_array($lists)) {
foreach($lists['data'] as $list) {
echo $list['id'] . " : " . $list['name'] . "\n";
} // foreach($lists as $list)
} else {
echo "No List for this account.\n";
}
echo "\n\nDone\n";
die(1);
}
/*
* If the user has asked for a list of segments for a list then just show that
* and bail. Fetch Mode assumes verbose mode.
*/
if ($fetchSegmentMode) {
$segments = $api->listStaticSegments($listId);
echo "\nList Segments for List ID: ".$listId."\n";
if (is_array($segments)) {
foreach($segments as $segment) {
echo $segment['id'] . " : " . $segment['name'] . "\n";
} // foreach($segments as $segment)
} else {
echo "No segments for this list.\n";
}
echo "\n\nDone\n";
die(1);
} // if (isset($options['v']))
/*
* Get a list of all available campaigns
*/
if ($verboseMode) {
echo "Fetching list of campaigns.\n";
}
$listofcampaigns = $api->campaigns();
/*
* If there is an error, deal with it.
*/
if ($api->errorCode) {
$message = "Unable to fetch a list of campaigns!";
$message .= "\n\tCode=".$api->errorCode;
$message .= "\n\tMsg=".$api->errorMessage."\n";
outputError($message,$verbose, $argv, $options);
die(-2);
} // if ($api->errorCode)
/*
* Get list of campaigns that match the query
*/
if ($verboseMode) {
echo "Filtering campaigns.\n";
}
foreach($listofcampaigns['data'] as $campaign) {
if (($campaign['list_id'] == $options['l']) &&
stripos($campaign['title'],$options['q'])!==false ) {
$campaignIds[] = $campaign['id'];
}
} // foreach($listofcampaigns['data'] as $campaign)
/*
* Now get every email address that opened each of the matching campaigns.
*/
if ($verboseMode) {
echo "Fetching opens for each matching campaign.\n";
}
foreach($campaignIds as $thisCampaign) {
$pageNo = 0;
$continueProcessing = true;
do
{
/*
* We fetch in 1000 page chunks. No particular reason other than 100
* seemed too samll.
*/
$addressesThatOpenedThisCampaign = $api->campaignOpenedAIM($thisCampaign, $pageNo,1000);
if ($api->errorCode==0) {
/*
* If for some reason we have no error but an empty array for the
* data then bail.
*/
if (count($addressesThatOpenedThisCampaign['data'])==0) {
break;
}
foreach($addressesThatOpenedThisCampaign['data'] as $open) {
$finalListofEmails[] = $open['email'];
}
} else if ($api->errorCode==301) {
/*
* A 301 is the campaign we are looking at has no stats. It
* probably hasn't been sent yet.
*/
$continueProcessing = false;
} else {
/*
* Something else happened. Let the user know but keep going.
*/
outputError("API Error Code:".$api->errorCode."\n\n",$verbose, $argv, $options);
}
$pageNo++;
} while ($continueProcessing===true);
}
/*
* Now add them to the list segment.
*/
if ($verboseMode) {
echo "Filtering out duplicate email addresses.\n";
} // if ($verboseMode)
$finalListofEmails = array_unique($finalListofEmails,SORT_STRING);
if ($verboseMode) {
echo "A total of ".count($finalListofEmails)." emails will be added to the segment.\n";
} // if ($verboseMode)
if (!$testMode) {
$output = array_chunk($finalListofEmails,1000);
if (is_null($segmentId)) {
$segmentId = $api->listStaticSegmentAdd($listId,$segmentTitle);
if ($verboseMode) {
echo "Created segment #{$segmentId}.\n";
} // if ($verboseMode)
}
foreach ($output as $chunk) {
$api->listStaticSegmentMembersAdd($listId,$segmentId,$chunk);
} // foreach ($output as $chunk)
} // if (!$testMode)
if ($verboseMode) {
echo "\nDone!\n";
} // if ($verboseMode)
die(0);
function outputHeader()
{
echo "\n";
echo "Mail Chimp List Segment Maker\n";
echo " Author: Cal Evans <[email protected]>\n";
echo " Blog : http://blog.calevans.com\n";
echo "\n";
return;
}
function outputError($message='',$verboseMode=false,$argv,$options)
{
if ($verboseMode) {
echo "\n\nError:\n";
echo "======\n";
echo $message."\n";
echo "Inputs:\n";
print_r($argv);
echo "\nOptions:\n";
print_r($options);
}
}