Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dgehrett
Last active May 17, 2016 16:43
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dgehrett/6828656 to your computer and use it in GitHub Desktop.
Save dgehrett/6828656 to your computer and use it in GitHub Desktop.
Source code for a Mandrill Dashing widget for use with the Shopify Dashing framework (https://github.com/Shopify/dashing). It shows detailed data about emails by tag. See more widgets we've made at https://github.com/Homefinder/dashing-widgets

##Preview

Simple Dashing widget (and associated job) to display data about tagged emails sent from Mandrill

Created at Homefinder.com

Check out other widgets we've made at https://github.com/Homefinder/dashing-widgets

##Usage To use this widget, copy mandrill.html, mandrill.coffee, and mandrill.scss into the /widgets/mandrill directory. Put the mandrill.rb file in your /jobs folder. Copy mandrill.png into the /assets/images directory.

Please refer to mandrill.erb for an example of how to include this widget in your dashboard.

##Settings Set your Mandrill API token in 'mandrill.rb'.

Optionally, you can install this running dashing install 6828656

class Dashing.Mandrill extends Dashing.Widget
onData: (data) ->
$(@node).removeClass("A B C D F")
if data.data.reputation
grade = switch
when data.data.reputation >= 90 then "A"
when data.data.reputation >= 80 then "B"
when data.data.reputation >= 70 then "C"
when data.data.reputation >= 60 then "D"
else "F"
$(@node).addClass(grade)
<li data-row="1" data-col="1" data-sizex="2" data-sizey="1">
<div data-id="some-mandrill-tag" data-view="Mandrill" data-title="Emails tagged as some-mandrill-tag"></div>
</li>
<h1 class="title" data-bind="title"></h1>
<table>
<tr>
<th>Stat</th>
<th>Total</th>
<th>Today</th>
<th>Last 7 days</th>
<th>Last 30 days</th>
</tr>
<tr>
<td><strong>sent</strong></td>
<td data-bind='data.sent'></td>
<td data-bind='data.stats.today.sent'></td>
<td data-bind='data.stats.last_7_days.sent'></td>
<td data-bind='data.stats.last_30_days.sent'></td>
</tr>
<tr>
<td><strong>deliverability</strong></td>
<td data-bind='data.deliverability'></td>
<td data-bind='data.stats.today.deliverability'></td>
<td data-bind='data.stats.last_7_days.deliverability'></td>
<td data-bind='data.stats.last_30_days.deliverability'></td>
</tr>
<tr>
<td><strong>opens</strong></td>
<td data-bind='data.open_pct'></td>
<td data-bind='data.stats.today.open_pct'></td>
<td data-bind='data.stats.last_7_days.open_pct'></td>
<td data-bind='data.stats.last_30_days.open_pct'></td>
</tr>
<tr>
<td><strong>clicks</strong></td>
<td data-bind='data.click_pct'></td>
<td data-bind='data.stats.today.click_pct'></td>
<td data-bind='data.stats.last_7_days.click_pct'></td>
<td data-bind='data.stats.last_30_days.click_pct'></td>
</tr>
</table>
<p class="updated-at" data-bind="updatedAtMessage"></p>
require 'net/http'
require 'json'
api_key = "YOUR API KEY HERE"
def calculate_rate(count, total_sent)
if total_sent > 0
"#{((count / total_sent.to_f) * 100).to_i}%"
else
"0%"
end
end
# tags
SCHEDULER.every '5m', :first_in => '1s' do |job|
uri = URI.parse("https://mandrillapp.com/api/1.0/tags/list.json")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({"key" => api_key})
response = http.request(request)
parsed_response = JSON.parse(response.body)
tags = []
# get list of tags
parsed_response.each do |tag|
tags << tag["tag"]
end
#now get detail info for each tag
tags.each do |tag|
uri = URI.parse("https://mandrillapp.com/api/1.0/tags/info.json")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({key: api_key, tag: tag})
response = http.request(request)
tag_stats = JSON.parse(response.body)
[tag_stats, tag_stats['stats']['today'], tag_stats['stats']['last_7_days'], tag_stats['stats']['last_30_days']].each do |stats|
total_deliveries = stats['sent'] - stats['hard_bounces'] - stats['soft_bounces']
stats["deliverability"] = calculate_rate(total_deliveries, stats['sent'])
stats["open_pct"] = calculate_rate(stats['unique_opens'], stats['sent'])
stats["click_pct"] = calculate_rate(stats['unique_clicks'], stats['sent'])
end
send_event(tag, {data: tag_stats})
end
end
.widget-mandrill {
background-image: url("/assets/mandrill_logo.png");
background-repeat: no-repeat;
background-position: center center;
.updated-at {
color: rgba(0, 0, 0, 0.3);
}
}
.A {
background-color: rgba(95,237,97,.8);
}
.B {
background-color: rgba(185,237,95,.8);
}
.C {
background-color: rgba(237,232,95,.8);
}
.D {
background-color: rgba(237,175,95,.8);
}
.F {
background-color: rgba(237,95,95,.8);
}
@kingm88
Copy link

kingm88 commented Feb 20, 2014

Hi,

I keep getting a "can't convert String into Integer" error on mandrill.rb.26

I can't figure out how to get past this. How did you enter a tag and get this to work?

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