Reputation: 577
In my django site, I have a page like: fubar.com/signup which posts to itself and then redirects to fubar.com/thank-you when successful (like when someone signs up for the site). I want to enforce a rule that would prevent the page from being reloaded over and over if the person goes back to it. I'm using the page to track conversions, and my numbers are off because people can (and do) hit the back button to get back to that page.
I've considered checking the HTTP_REFERER attribute from HttpRequest.META, but I'm not sure how reliable that will be (if it could be formatted differently in different clients, or not provided at all).
Is there a standard way to do this? Would the HTTP_REFERER attribute be reliable?
Upvotes: 1
Views: 227
Reputation: 9242
Checking HTTP_REFERER
is pretty reliable; check the referrer in your view:
data = {
'foo': 'bar',
...
}
if request.META.get('HTTP_REFERER', False) == reverse('signup'):
data.update({'conversion': True})
render(request, 'thanks.html', data)
then use that information in your template to determine whether to detect a conversion:
{% if conversion %}<script type='text/javascript'> // record conversion</script>{% endif %}
Although Yuri's answer is more generally correct (as it ensures that each conversion will be recorded at least once, as long as the user visits the thank-you page within a reasonable time-frame), it will involve a hit to session storage every time. Use either, or both ;)
Upvotes: 1
Reputation: 15144
A possible solution is to store a value in the user's session.
It should be pretty easy assuming you have a separate view for each page.
Set the session variable in the signup view, after a successful signup (before redirecting):
request.session['conversion'] = True
And then add the value to the context in the thank you page:
return direct_to_template(request, template_name, {
'conversion': request.session.pop('conversion', False),
})
Then you simply wrap the conversion tracking code in an if statement in the template:
{% if conversion %}
{# Conversion tracking script #}
{% endif %}
I think that this is more reliable then checking the referrer.
Upvotes: 1