Who Loves You: Getting Open Rates Via The API

by Alex Durzy on August 27, 2009 · 1 comment

Open Door

This post only applies to Legacy Version 3 of the API

You know you can update contacts and schedule deliveries through the Bronto API, but what about access to all that great statistical data such as opens and clicks for your contacts and deliveries?  Let’s look at the readOpens function and how we might use it to determine who loves you, or at least who loves to open your messages, the most.

Open Sesame

The readOpens function allows us to gather opens at a very granular level, either by contact or delivery id. Using the results you get back from this function, you can generate a list of the contacts that opened the most emails. You will need to be aware of the difference between total opens and unique opens. If you’ve used the Bronto web application and looked at any of the message reports, you’re most likely already familiar with this concept. A contact can open an email multiple times, and depending on your particular use case, you may or may not be interested in this. For the purposes of this exercise, we’ll be looking at unique opens. With a unique open, it doesn’t matter to us here if a contact opened a single email 20 times, we only want to count the open once.

First, let’s outline what we need to do in order to accomplish our task, and then we’ll look at an example in PHP.

  1. Login to the API.
  2. Retrieve a list of deliveries for a date range.
  3. Retrieve the opens for each delivery.
  4. Get the contacts that opened the delivery.
  5. Aggregate the total number of emails opened by each contact.

Feel free to download this script, shown below, and begin tweaking it for your own requirements.

Getting Opens in PHP


/**
* login to the Bronto API
*
* $username Bronto username
* $password Bronto password
* $sitename Bronto sitename
*
* return binding
*/
function login($username, $password, $sitename)
{
$BRONTO_WSDL = 'http://api.bronto.com/?q=mail_3&wsdl';
// Set some defaults
set_time_limit(600);
ini_set("memory_limit", "150M");
ini_set("default_socket_timeout", 300);
ini_set("soap.wsdl_cache_enabled", 0);

// creates a handle to bronto API to be used in all subsequent calls
$bapi = new SoapClient( $BRONTO_WSDL, array(“trace” => 1) );

// call login operation to obtain a sessionId
$parameters = array(“username” => $username,
“password” => $password,
“sitename” => $sitename);
$result = $bapi->login($parameters);

// just return null if there was a problem
if(!$result->return->success)
{
return(null);
}

// setup soap header which will be used on all calls to the api
$session_header = new SoapHeader(“http://api.bronto.com”,   ”sessionHeader”,

array(“sessionId” => $result->return->sessionId));
$bapi->__setSoapHeaders(array($session_header));
return($bapi);
}
/**
* read the deliveries for a date range.
*
* $bapi – binding to bronto api
* $start_date – the starting date
* $end_date – the ending date
*
* date/time format: UTC timezone “yyyy-mm-dd hh:mm:ss”
* hh:mm:ss is not required to specify a date
*
* return an array of delivery objects
*/
function read_deliveries($bapi, $start_date, $end_date)
{
$attributes = array();
$attributes["status"] = true;

$filter = array();
$filter["criteria"] = array();

$criteria1 = array();
$criteria1["attribute"] = “start”;
$criteria1["comparison"] = “>”;
$criteria1["value"] = array();
$criteria1["value"]["value"] = $start_date;
$criteria1["value"]["type"] = “date”;
$filter["criteria"][] = $criteria1;

$criteria2 = array();
$criteria2["attribute"] = “start”;
$criteria2["comparison"] = “<”;
$criteria2["value"] = array();
$criteria2["value"]["value"] = $end_date;
$criteria2["value"]["type"] = “date”;
$filter["criteria"][] = $criteria2;

$filter["operator"] = “and”;

$parameters = array(“attributes”=>$attributes, “filter”=>$filter);
$results = $bapi->readDeliveries($parameters);

return($results->return->deliveries);
}

/**
* count the unique opens for the given list of deliveries
*
* $bapi – binding the bronto api
* $deliveries – list of delivery objects
*
* return array of contact id associated with the number of opened emails
*/
function count_unique_opens_for_deliveries($bapi, $deliveries)
{
// ths list of contacts with the unique open count
$unique_opens = array();

// print a “working” message and …
echo “Reading opens”;
foreach($deliveries as $delivery)
{
$opens = read_opens_for_delivery($bapi, $delivery->id);
if($opens)
{
echo “.”;
// get a list of unique contacts that opened this delivery
$contacts = array();
foreach($opens as $open)
{
//if(!array_key_exists($open->contactId, $contacts))
if(!in_array($open->contactId, $contacts))
{
//$opens_for_delivery[$open->contactId] = 1;
$contacts[] = $open->contactId;
}
}
// aggregate contacts list to unique_opens list
foreach($contacts as $contact)
{
$unique_opens[$contact] += 1;
}
}
}
echo “n”;
return($unique_opens);
}
$bapi = login(“username”, “password”, “sitename”);
if(!$bapi)
{
exit(“Error logging in. Exiting…n”);
}

$deliveries = read_deliveries($bapi, "2009-08-01", "2009-08-10");
$unique_opens = count_unique_opens_for_deliveries($bapi, $deliveries);
print_r($unique_opens);

Now What?

We now have a list of contact ids along with the number of emails each contact opened. You can use these ids for other operations, such as managing the lists to which the contact is subscribed, or sending the contact a message. Perhaps you want to base a future email on the number of times a contact has opened previous emails. If you actually dig a little deeper into the data that is returned by the readOpens function, you will also see that you can get the IP address from which the open originated. Maybe you want to get a sense for where your emails are being opened most often? You can use this data along with an ip address lookup service to generate this.

The other way to retrieve open information is by contact id.  If you have a specific contact or list of contacts in which you are interested, you may decide to use this method instead.  Again, you will need to be aware of unique verses total opens, but the process will be fairly similar.  What other information is available you ask?  Next time we’ll look at link/click data so you can see who loves you enough to click a link.

Alex Durzy
Support Engineer at Bronto

Related posts:

  1. Spring Cleaning: Clearing Contacts From Lists This post only applies to Legacy Version 3 of the...
  2. Washing With SOAP: Part 3 This post only applies to Legacy Version 3 of...
  3. Washing With SOAP: Part 2 This post only applies to Legacy Version 3 of the...

{ 1 trackback }

Add Contacts Using the API
02.18.10 at 1:41 pm

{ 0 comments… add one now }

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post: