But why the middleware?
Django middleware is a good place because it's called for all request and response cycle.
The upside to this is:
- It's easy to customise. Django middleware is just a plain Python class with some methods.
- The Django middleware is well-documented.
- Allows us to setup whatever rules like exclude errors and only track HTTP 200 responses.
- We can setup tracking to be asynchronous. This way, our pages are not at the mercy of an external resource.
Writer's note: If your analytics needs are simple, you can install django-google-analytics and follow Usage #2 - Middleware + Celery.
There are a couple of downsides to this though:
- We could be impacting performance in a big way if the analytics middleware is doing too many things or worst, is misconfigured.
- Not as bad as #1 but if we went down the asynchronous path, we will end up with a Celery server with some message broker like RabbitMQ on the tech stack. Another thing we'll have to manage.
TL:DR For analytics with Django, HTML tag slows down pages and could be blocked; better to use a middleware approach.