Django/Python — How to Track Email Open Rates

Hola!

I just finished some email tracking software, and I’m going to explain very quickly how it works.

My newsletter sender appends a specific image to the end of the email, with a specific get request that identifies the recipient.

Lets say I have a subscriber model, full of emails. I send the email: “Hello”

subscriber = Subscriber(email='yuji@anuvawines.com', name='Yuji Tomita')
subject = 'testing open tracking'
body = "hello'
new_body = body + '<img src="/open-tracking/?id=%s" />' % (subscriber.id) 
send_html_mail(subject, new_body, "from@anuvawines.com", [subscriber.email])

This sends an email with an image embedded that identifies the subscriber.

Now, for the view to handle the image request. It must return a valid image or else your html email will often have a broken image icon. Here’s how to pass back an image in django:

def track-open-emails(request):
     if 'id' in request.GET:
        #do something with the id, which tells you that specific subscriber has opened the email.
     #do something to record that an email has been opened.
     image_data = open("/path_to_image/", 'rb').read()
     return http.HttpResponse(image_data, mimetype="image/png")

done!

6 thoughts on “Django/Python — How to Track Email Open Rates

  1. Hy Yuri!
    I know it’s an old post, but i hope you will answer my post.

    1st of all thanx for the method, i’ll try it soon.
    But there is a question which does not leave my head to rest:
    what if the reciver mail client (outlook etc.) disable any img default.

    Is this method tracking when the images are disabled in a client?

    Thanks for your answer.

    best regards
    Sumia01

    1. Hey there,

      Sorry but I’m not familiar with other ways of tracking email opens – they would be a combination of the image technique and other methods listed in the wiki page.

      I would ask stackoverflow.com or google more ; )

      Good luck!

  2. Hi all, default,the Email doesn’t allow to auto the image , so we can not tracking .
    If you have another solution , please help me to share it.
    Thanks.

  3. Hey Yuji,

    I am trying to implement the same in my code, but struggling to implement because i am very new to python and Django. Will be very helpful if you can share the complete code to me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s