middleware for tracking stats