In the last blog post, we have learnt how to send emails via Mandrill API.

Today, we are going to learn about one of the important features of Mandrill API. We will find Mandrill API-connected user’s statistics via Mandrill API Key. We will also find all the message history sent by that particular user. Such information could be helpful for your Marketing Analytics strategy.

To get the user information, Mandrill API have provided a simple code. Just to use the following code in the PHP file.

 //Pass Mandrill API key to Mandrill library
$mandrill = new Mandrill($key);
//Get Mandrill API-connected user's info
$result = $mandrill->users->info();

Watch the live demo or download code from the link given below

get user statistics via mandrill api


Note: – We have introduced a very effective and powerful online email management service name MailGet. MailGet allows you a simple smtp configuration for sending bulk emails through Mandrill and manage your emails at very low cost.

The above-mentioned code will return an arrays containing all the information of the user having that particular API key.

  • username:  username of the user created for SMTP authentication.
  • created_at: Mandrill Account creation date and time ( YYYY-MM-DD HH:MM:SS format ).
  • public_id: a unique, permanent identifier for this user.
  • reputation: the reputation of the user on a scale from 0 to 100, with 75 generally being a “good” reputation.
  • hourly_quota: Maximum number of  emails that Mandrill will send for this user.
  • backlog: the number of emails that are queued for delivery due to exceeding your monthly or hourly quotas
  • stats:   summary of the account’s sending stats.
    • today:  stats for this user so far today
      • sent: total number of emails sent for this user so far today.
      • hard_bounces: total number of emails hard bounced for this user so far today.
      • soft_bounces: total number of emails soft bounced for this user so far today.
      • rejects: total number of emails rejected for this user so far today.
      • complaints:  total number of emails spam complaints for this user so far today.
      • unsubs: the number of unsubscribes for this user so far today.
      • opens: the number of times emails have been opened for this user so far today
      • unique_opens: the number of unique opens for emails sent for this user so far today.
      • clicks: the number of URLs that have been clicked for this user so far today.
      • unique_clicks: the number of unique clicks for emails sent for this user so far today.
    • last_7_days: Same information as available for today but, of last 7 days.
    • last_30_days: Same information as available for today but, of last 30 days.
    •  last_60_days: Same information as available for today but, of last 60 days.
    • last_90_days: Same information as available for today but, of last 90 days.
    • all_time: Same information as available for today but, of the lifetime of the user’s account.

Let’s begin the scripting part.

First we need to create the directory structure in the same way as  we made in our last blog post while sending Emails (i.e create separate folders for Mandrill API library, CSS,  js and other files).

Note: – If you are reading our Mandrill API blogs first-time, then there is no need to worry. Read our previous published blog “Send Email via Mandrill API Using PHP” in which directory structure is well described.

Now, create index.php file in the root directory and paste the Php code given below in it.

index.php

Allow a user to insert Mandrill API Key and check whether the key is valid or Invalid. If the key found valid, then it retrieve and show all the accont information of the user having that particular API key otherwise show error messages.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Get User Statistics via Mandrill API</title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<script src="js/jquery.js"></script>
</head>
<body>
<?php

//include Mandrill library file
include ("lib/Mandrill.php");
$error_msg = "";
?>
<div id="main">
<h1>Get Mandrill API-Connected User's Statistics </h1>
<div id="login">
<h2>Mandrill User's Info Search Box</h2>
<form action="" method="POST">
<label>Mandrill API-Key : </label> <input type="text" name="mandrill_api_key" class="mandrill-api-key" placeholder="Enter Mandrill API Key"/>
<input type="submit" value="Search" id="submit"/>
</form>
</div>
<?php
if (isset($_POST['mandrill_api_key'])) {
$key = $_POST['mandrill_api_key'];

//Pass user provided Mandrill API key to Mandrill library
$mandrill = new Mandrill($key);

//Get Mandrill API-connected user's info
$result = $mandrill->users->info();
if (isset($result['message'])) {
$error_msg = $result['message'];
echo "<script>alert('" . $error_msg . "');</script>";
} else {
?>

<!-- Result div start -->
<div class="result">
<h1 class="res">Result</h1>
<div class="user_info">
<ul class="info">
<li class="index_info"> Username </li><li class='val'> : <?php echo $result['username'] ?></li>
<li class="index_info"> Created At </li><li class='val'> : <?php echo $result['created_at'] ?></li>
<li class="index_info"> Public ID </li><li class='val'> : <?php echo $result['public_id'] ?></li>
<li class="index_info"> Reputation </li><li class='val'> : <?php echo $result['reputation'] ?></li>
<li class="index_info"> Email's allowed to send per hour </li><li class='val'> : <?php echo $result['hourly_quota'] ?></li>
</ul>
<div id="tabs-container">
<ul class="tabs-menu">
<h2 class="msg_history">Message History</h2>
<li class="current"><a href="#tab-1">Today</a></li>
<li><a href="#tab-2">Last 7 day's</a></li>
<li><a href="#tab-3">Last 30 day's</a></li>
<li><a href="#tab-4">Last 60 day's</a></li>
<li><a href="#tab-5">Last 90 day's</a></li>
<li><a href="#tab-6">All</a></li>
</ul>
<div class="tab">
<div id="tab-1" class="tab-content">
<ul class="tab-info">
<?php
foreach ($result['stats']['today'] as $key => $val) {
echo "<li class='index'>" . $key . "</li><li class='val'> : " . $val . "</li><br/>";
?>
<?php } ?>
</ul>
</div>
<div id="tab-2" class="tab-content">
<ul class="tab-info">
<?php
foreach ($result['stats']['last_7_days'] as $key => $val) {
echo "<li class='index'>" . $key . "</li><li class='val'> : " . $val . "</li>";
?>
<?php } ?>
</ul>
</div>
<div id="tab-3" class="tab-content">
<ul class="tab-info">
<?php
foreach ($result['stats']['last_30_days'] as $key => $val) {
echo "<li class='index'>" . $key . "</li><li class='val'> : " . $val . "</li>";
?>
<?php } ?>
</ul>
</div>
<div id="tab-4" class="tab-content">
<ul class="tab-info">
<?php
foreach ($result['stats']['last_60_days'] as $key => $val) {
echo "<li class='index'>" . $key . "</li><li class='val'> : " . $val . "</li>";
?>
<?php } ?>
</ul>
</div>
<div id="tab-5" class="tab-content">
<ul class="tab-info">
<?php
foreach ($result['stats']['last_90_days'] as $key => $val) {
echo "<li class='index'>" . $key . "</li><li class='val'> : " . $val . "</li>";
?>
<?php } ?>
</ul>
</div>
<div id="tab-6" class="tab-content">
<ul class="tab-info">
<?php
foreach ($result['stats']['all_time'] as $key => $val) {
echo "<li class='index'>" . $key . "</li><li class='val'> : " . $val . "</li>";
?>
<?php } ?>
</ul>
</div>
</div>
</div>
</div>
</div>

<!-- Result div End -->
<?php
}
}
?>
</div>
<script>
jQuery(document).ready(function() {
$("#submit").click(function(event) {
var Mandrillkey = jQuery('.mandrill-api-key').val();
if (Mandrillkey == "") {
event.preventDefault();
alert('Please insert Mandrill API Key!!!');
}
});
$(".tabs-menu a").click(function(event) {
event.preventDefault();
$(this).parent().addClass("current");
$(this).parent().siblings().removeClass("current");
var tab = $(this).attr("href");
$(".tab-content").not(tab).css("display", "none");
$(tab).fadeIn();
});
});
</script>
</body>
</html>

In style.css ( created inside CSS folder ), paste the css code given below.

style.css

Includes basic styling of HTML elements.

@import url(http://fonts.googleapis.com/css?family=Raleway);

#main{
width:960px;
margin:50px auto;
font-family:raleway;
}
h2{
background-color: #FEFFED;
text-align:center;
border-radius: 10px 10px 0 0;
margin: -10px -40px;
padding: 15px;
}
hr{
border:0;
border-bottom:1px solid #ccc;
margin: 10px -40px;
margin-bottom: 30px;
}
ul{
text-decoration: none;
}
ul li{
display: inline-block;
}
#login{
width:300px;
border-radius: 10px;
font-family:raleway;
border: 2px solid #ccc;
padding: 10px 40px 25px;
margin: 70px auto 0 auto;
}
input[type=text],input[type=email]{
width:100%;
padding: 10px;
margin-top: 8px;
border: 1px solid #ccc;
padding-left: 5px;
font-size: 16px;
font-family:raleway;
}
input[type=submit]{
width: 100%;
background-color:#FFBC00;
color: white;
border: 2px solid #FFCB00;
padding: 10px;
font-size:20px;
cursor:pointer;
border-radius: 5px;
margin-bottom: 15px;
}
a#demo_key {
float: right;
margin: 10px;
}
textarea{
width: 100%;
}
div#main h1 {
text-align: center;
}
h1.res {
margin-top: 51px;
}
div.user_info {
width: 750px;
height: 625px;
background-color: #FEFFED;
padding: 5px;
border:1px solid #D5D89B;
border-radius: 10px 10px 5px 5px;
margin: 30px auto;
}
.info li {
padding: 5px;
font-size: 18px;
}
.tabs-menu {
height: 30px;
float: left;
clear: both;
}
.tabs-menu li {
height: 30px;
line-height: 30px;
float: left;
background-color: #FFBC00;
border: 1px solid #d4d4d1;
}
.tabs-menu li.current {
position: relative;
background-color: #fff;
z-index: 5;
}
ul.tab-info {
margin-top: 90px;
}
li.index_info{
width: 50%;
text-align: left;
font-weight: bold;
}
li.index {
width: 30%;
text-align: left;
font-weight: bold;
}
ul.tab-info li {
padding: 5px;
font-size: 16px;
}

.tabs-menu li a {
padding: 10px;
text-transform: uppercase;
color: #fff;
text-decoration: none;
}
.tabs-menu .current a {
color: #2e7da3;
}
.tab {
border: 1px solid #d4d4d1;
background-color: #fff;
margin-bottom: 20px;
width: auto;
}
.tab-content {
text-align: center;
padding: 20px;
display: none;
}
ul.tabs-menu {
width: 100%;
}
#tab-1 {
display: block;
}
h2.msg_history{
text-align:center;
margin-top: -10px;
margin-bottom: 0;
padding: 15px;
margin-left: -120px;
background: none;
}

Now, run the script and have fun!!!

Conclusion :

Hope you have enjoyed learning Mandrill API’s feature. Keep in touch with us, we will be come back soon with another feature of Mandrill API. You can send us your feedback and share your experience about this blog in the space given below 🙂