Django external links

I made this a couple of weeks ago for a personal project. Django external links is a pluggable app that keeps track of the clicks done to external links on your site.

To install just set up an URL where should external links should be redirected to, this endpoint will keep track of the hit and redirect to the destination url

    urlpatterns += patterns(''
        url(r'^external/', include('external_links.urls')),
    )

Now you have two ways to use it, via the inline {% external %} template tag or the blog tag {% externalblock %}

The “external” tag translates a regular link into a redirected link, from <a href="{% external "http://example.com" %}">Other site</a> to <a href="/external/?link=http%3a%2f%2example.com">Other site</a>

The “externalblock” will do the same for a big block of text, it will perform the same transformation on all the anchor tags found inside, this is useful for blogposts, articles and such.

1 Comment so far

  1. Rudy

    August 3rd, 2009

    Just wanted to pass along a mod I made to the “external” template tag. Essentially I wanted the tag to also have it automatically add the Google Analytics onClick method to the <A tag.

    It’s a straight forward hack. If you like it, maybe you can find a way to make this optional.


    @register.simple_tag
    def external(link):
    """
    Replaces an external link with a redirect to
    keep track of the clicked link
    """
    redirect_endpoint = reverse('external_link')
    params = urlencode({'link': link})

    # pageTracker was added to support the onclick version for Google Analytics - rm 8/3/2009
    plink = link.replace('http://','')
    plink = plink.split('?')[0]
    pageTracker = ‘” onClick=”javascript: pageTracker._trackPageview(\’/outgoing/%s\’);’%(plink)

    return redirect_endpoint + ‘?’ + params + pageTracker

    see: http://www.google.com/support/googleanalytics/bin/answer.py?answer=55527


Add yours

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>