开发者

django pixel tracking

I'm using django to do a pixel tracker on an email

Is it easy to return an actual image from a dj开发者_如何学Cango view (and how would this be done?) or is it easier to just return a redirect to the url where the actual image lives?


Since this was the first result on my google search and the best answer is buried in the link by Daniel (but not mentioned as the best), I figured I would just post the answer so nobody is tempted to return a blank response which as a Michael points out is not ideal.

The solution is to use a standard view and return an HttpResponse with the raw data that makes up a single pixel gif. Not having to hit the disk or redirect is a huge advantage.

Note that the url pattern uses the tracking code as the image name so there is no obvious ?code=jf8992jf in the url.

from django.conf.urls import patterns, url
from emails.views.pixel import PixelView

urlpatterns = patterns('',
    url(r'^/p/(?P<pixel>\w+).gif$', PixelView.as_view(), name='email_pixel'),
)

And here is the view. Note that it uses cache_control to prevent requests from running wild. Firefox (along with many email clients) for instance will request the image twice every time for some reason you probably don't care about but need to worry about. By adding max_age=60 you will just get one request per minute.

from django.views.decorators.cache import cache_control
from django.http.response import HttpResponse
from django.views.generic import View   

class PixelView(View):

    @cache_control(must_revalidate=True, max_age=60)
    def get(self, request, pixel):
        """
        Tracking pixel for opening an email
        :param request: WSGIRequest
        :param pixel: str
        :return: HttpResponse
        """

        # Do whatever tracking you want here

        # Render the pixel
        pixel_image = b'\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\x00\x00\x00\x21\xf9\x04\x01\x00\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3b'
        return HttpResponse(pixel_image, content_type='image/gif')


You don't need an actual image for a tracker pixel. In fact, it's better if you don't have one.

Just use the view as the source for the image tag, and have it return a blank response.


Django has a static file helper that could be used to serve up the image, but it is not recommended because of performance. I believe that having a view that does the bookkeeping to track the pixel, then redirect to a url that serves the actual image via the webserver is going to give you the best performance.


Python2.x:

from django.http import HttpResponse

PIXEL_GIF_DATA = """
R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
""".strip().decode('base64')

def pixel_gif(request):
    return HttpResponse(PIXEL_GIF_DATA, content_type='image/gif')

Python3.x:

import base64
from django.http import HttpResponse

PIXEL_GIF_DATA = base64.b64decode(
    b"R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7")

def pixel_gif(request):
    return HttpResponse(PIXEL_GIF_DATA, content_type='image/gif')

Source: https://gist.github.com/simonw/10235798

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜