Question

My website generates some short URLs when users share a link (ex: http://futureo.us/l/ixjF).

These short URLs redirect the user to the original content I'm linking to. Before redirecting, the app renders a page that contains only Google Analytics javascript code.

Currently my handler code looks like this:

class PostHandler(handler.Handler):
def get(self, code):
    #strip URL shortcode
    code = code.strip('/')
    #grab URL based on shortcode
    url = scripts.urlshort.getURL(code)
    if url:
        self.render('tracking.html')
        self.redirect(str(url))
    else:
        self.write('Code not FOUND.')

This solution isn't working. GA is not registering pageviews to these short links. I would alo like to see who where the referrers to these short-links.

Any ideas how I could fix this?

Was it helpful?

Solution

I believe your problem is that you're adding an HTML tracking code, in a response that has HTTP redirection. Probably the HTTP redirection is processed earlier than the HTML, if the latter is evaluated at all.

Seems to me that the best solution would be to track the redirects on the server side rather than on the client side. As these are redirects anyway, you don't need to track client-only data such as time spent on page, page events etc. Tracking the redirect would be most accurate and simpler if done in the python code. (I don't know, though, of a way to use the google analytics tools for tracking these; for my uses I just track the redirects in an NDB model).

Another solution, which might be slower on the user experience, is to avoid using HTTP redirect (self.redirect) and instead put a javascript client-side redirect which will be evaluated after the tracking code.

window.location = "{{url}}";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top