Guest User

Untitled

a guest
Dec 12th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1.  
  2. At AMEE, we've been using Sendgrid and KissMetrics to take care of delivering email and understand how our apps are used in more detail for a while now (we'd never be able to deliver the same level of service internally as they can, and they get better for free), but as is often the case when working with disparate third party services, they don't _always_ work together how you want.
  3.  
  4. One of the reasons we use these two services however, is that they provide a rich enough API to combine them to plug gaps like we mentioned above.
  5.  
  6. For example, we use Sendgrid for transactional email, and it does a sterling job when it comes to actually making sure email ends up in the right inbox. But if we wanted to see what a user did with an email it had arrived, and log that for analysis later, we could't find a simple way to do this.
  7.  
  8. Likewise with KissMetrics - it's straightforward enough to create a libary of events in the browser to log, that you can then combine in groups to build reports on how well a certain conversion funnel is performing. But if you want to track things happening outide a user's browser (say, deep in the innards of your own application, or in an hosted email service) you'll need to roll your sleeves up and start monkeying around with the API.
  9.  
  10. ### Keeping you POSTed with Sendgrid
  11.  
  12. Sendgrid has made a name for itself sending billions and billions of emails, but it doesn't retain accessible logs of the state of every email sent using its service ever, but the API doesn't really let you ask a question like: "what happened to every email sent to joe.bloggs@foo.com ?".
  13.  
  14. What it _does_ do however, is provide an event API, which can hit a predefined endpoint, to send along updates about the progress of a message being sent to a particular address, as it gets queued for delivery, marked as spam, bounced, delivered successfully, opened, and so on.
  15.  
  16. Of course, for any of this to work, you actually need somewhere to send these messages to.
  17.  
  18. ### Sending messages to KissMetrics
  19.  
  20. Fortunately, Kissmetrics has a refreshingly simple API, so setting up a simple Sinatra app, `km-sinatra`, to get the two services running together was relatively painless, once we'd worked out how to get sinatra and rspec playing nicely together.
  21.  
  22. Here's a high level view of what it looks like:
  23.  
  24.  
  25.  
  26. ### What does the code look like though?
  27.  
  28. The first pass for making the code was as pretty much as simple as this:
  29.  
  30. post "/" do
  31. if params[:category] == "ProjectName"
  32. # setup connection with KissMetrics, passing in API key, giving us an object, `KM` to work with
  33. initialise_km
  34.  
  35. # identify our user, based on the email address passed along by sendgrid
  36. KM.identify(params[:email])
  37.  
  38. # log the appropriate event for our user
  39. case params[:event]
  40. when 'processed'
  41. KM.record('processed for delivery')
  42. when 'delivered'
  43. KM.record('received mail')
  44. when 'open'
  45. KM.record('viewed mail')
  46. when 'click'
  47. KM.record('clicked link')
  48. when 'bounce'
  49. KM.record('bounced mail')
  50. when 'dropped'
  51. KM.record('drop mail')
  52. end
  53. end
  54. # give us an ok response with Sinatra
  55. 200
  56. end
Add Comment
Please, Sign In to add comment