2

I have Django event logging application with middleware, which logs user page views. Currently, if response code is 200, log "User X visited page Y" is saved, but in case of redirect the log should be "User X has been redirected to page Y".

Is it possible to determine if response (200) occurred after 302 response redirect?

EMiDU
  • 654
  • 1
  • 7
  • 24

1 Answers1

0

Not really, at least in any official way. HTTP requests are independent of each other. You cant tell that one request followed another. That is a reason if you need to maintain state between pages, you end up using sessions and passing session IDs around.

For your purposes using session ID to track pages is not reliable since a user can have multiple pages open.

The only semi-reliable solution I can think of is appending a tracking querystring to the URL upon redirects.

For example if some view processing request to /foo/ returns a redirect to /bar/ in your middleware you change that URL to /bar/?tracking=<something random>. The random part can be a uuid or something similar. Then when the user will go to that page, you can match the random bit and hence correlate that the request came from the original page /foo/. Note that in order for this to work the random bit will have to be unique for all requests.

Whether you should use above approach, probably not. Its probably not very reliable and probably has many edge cases where it will break. Maybe you can change your requirements to reflect HTTP-nature a bit better hence you will not need to come up with hacks to do what you are trying to do?

miki725
  • 27,207
  • 17
  • 105
  • 121