Skip to content

Instantly share code, notes, and snippets.

@jamescridland
Last active October 15, 2024 14:36
Show Gist options
  • Save jamescridland/1f4ea72fbd262fa31850ccfd5a54df0a to your computer and use it in GitHub Desktop.
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
<?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);
}
//-----------------------------------------------------------//
?>
@jacobrosenfeld
Copy link

I can't get this to work currently. Any chance you've updated it?

@jamescridland
Copy link
Author

@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!

@jacobrosenfeld
Copy link

jacobrosenfeld commented Oct 15, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment