Last active
October 15, 2024 14:36
-
-
Save jamescridland/1f4ea72fbd262fa31850ccfd5a54df0a to your computer and use it in GitHub Desktop.
An additional API for Sendy, that offers a report for a specific sent campaign
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php include('../_connect.php');?> | |
<?php include('../../includes/helpers/short.php');?> | |
<?php | |
/* | |
---Little helper function from [email protected] for reporting | |
Put this file in a new folder within the /api/ folder, called "reporting", and call it "reports.php". (Or whatever you like). | |
Call by POST to api/reporting/reports.php with the following mandatory elements | |
'api_key' => (your API key) | |
'brand_id' => 1 | |
'label' => (the campaign name) | |
(Using the campaign name allows you to programmatically call a campaign without knowing its campaign ID) | |
The data return is in JSON and looks like the following: | |
{"total_opens":361,"unique_opens":208,"country_opens":{"US":127,"EU":47,"GB":57,"AP":19,"CZ":1,"":3,"DE":3,"IE":16,"AU":48,"SE":6,"ES":2,"CH":1,"FR":4,"SI":2,"CA":14,"NL":2,"MV":1,"NZ":2,"AE":1,"IN":1,"BE":1,"JP":2,"BR":1},"total_sent":"341","brand_id":"1","label":"podnews 2017-08-07"} | |
total_opens: the total opens figure, visible in your dashboard | |
unique_opens: de-duplicated opens figure | |
country_opens: an array with individual countries opened out. Note - this is based on total_opens, not unique | |
total_sent: the total sent for this campaign | |
brand_id: the brand ID you sent | |
label: the label you requested | |
*/ | |
//-------------------------- ERRORS -------------------------// | |
$error_core = array('No data passed', 'API key not passed', 'Invalid API key'); | |
$error_passed = array( | |
'Brand ID not passed' | |
, 'Label not passed' | |
, 'This combination of Brand ID and Label does not exist' | |
); | |
//-----------------------------------------------------------// | |
// | |
//--------------------------- POST --------------------------// | |
//api_key | |
$api_key = isset($_POST['api_key']) ? mysqli_real_escape_string($mysqli, $_POST['api_key']) : null; | |
//brand_id | |
$app = isset($_POST['brand_id']) && is_numeric($_POST['brand_id']) ? mysqli_real_escape_string($mysqli, (int)$_POST['brand_id']) : null; | |
//label | |
$label = isset($_POST['label']) ? mysqli_real_escape_string($mysqli, $_POST['label']) : null; | |
//-----------------------------------------------------------// | |
//----------------------- VERIFICATION ----------------------// | |
//Core data | |
if($api_key==null && $app==null && $label==null) | |
{ | |
echo $error_core[0]; | |
exit; | |
} | |
if($api_key==null) | |
{ | |
echo $error_core[1]; | |
exit; | |
} | |
else if(!verify_api_key($api_key)) | |
{ | |
echo $error_core[2]; | |
exit; | |
} | |
//Passed data | |
if($app==null) | |
{ | |
echo $error_passed[0]; | |
exit; | |
} | |
else if($label==null) | |
{ | |
echo $error_passed[1]; | |
exit; | |
} | |
//So, here we are, I think. | |
//We've been passed a brandID and a label. | |
$app = trim($app); | |
$q = 'SELECT title,to_send,opens FROM campaigns WHERE app = '.$app.' AND label = "'.$label.'";'; | |
$r = mysqli_query($mysqli, $q); | |
if (mysqli_num_rows($r) == 0) | |
{ | |
echo $error_passed[2]; | |
exit; | |
} | |
else | |
{ | |
$data = mysqli_fetch_assoc($r); | |
$opens = stripslashes($data['opens']); | |
$opens_array=explode(',', $opens); | |
$data['total_opens']=count($opens_array); | |
$data_opens=array(); | |
foreach ($opens_array as $open) { | |
list($id,$country)=explode(':',$open); | |
@$data_opens[$id]++; | |
@$data_country[$country]++; | |
} | |
$data['unique_opens']=count($data_opens); | |
$data['country_opens']=$data_country; | |
//tidy up the data a little | |
$data['total_sent']=$data['to_send']; | |
$data['brand_id']=$app; | |
$data['label']=$label; | |
$data['subject']=$data['title']; | |
unset($data['to_send']); | |
unset($data['opens']); | |
unset($data['title']); | |
echo json_encode($data); | |
} | |
//-----------------------------------------------------------// | |
?> |
I actually worked on it and updated it based on the built in sendy apis and got it working. I put it up in a repo jacobrosenfeld/sendy-api-addons
… On Oct 15, 2024, at 2:27 AM, James Cridland ***@***.***> wrote:
@jamescridland commented on this gist.
@jacobrosenfeld <https://github.com/jacobrosenfeld> Looks like I've updated this function, yes (perhaps I needed to after some updates happened to Sendy). I've updated this gist. Shout if that fixes things!
—
Reply to this email directly, view it on GitHub <https://gist.github.com/jamescridland/1f4ea72fbd262fa31850ccfd5a54df0a#gistcomment-5234871> or unsubscribe <https://github.com/notifications/unsubscribe-auth/AFBBNHFK7R552WQTFHKYLALZ3SYT5BFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVA4TEOJZGE3DEMVHORZGSZ3HMVZKMY3SMVQXIZI>.
You are receiving this email because you were mentioned.
Triage notifications on the go with GitHub Mobile for iOS <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jacobrosenfeld Looks like I've updated this function, yes (perhaps I needed to after some updates happened to Sendy). I've updated this gist. Shout if that fixes things!