I created a plug that records the number of accesses to the pages requested by the client after reading a blog post.
However, since my service is based on a live view, pages accessed using patch or navigate are not invoking the plug, and the access status is not properly recorded. I also considered on_mount, but it was not a perfect solution because on_mount is not invoked in the case of patch.
Is there a way to see requests exchanged through a socket in the middle like a plug?
I welcome any other solutions if there are any.
This is code of my plug
defp count_view(conn, _opts) do
if conn.status == 200 do
path = "/" <> Enum.join(conn.path_info, "/")
Chat.ViewCounter.bump(path)
end
conn
end
Regarding hooking into both LiveView navigate and patch interactions, the lifecycle callback handle_params/3 should do the trick.
The handle_params/3 callback is invoked after mount/3 and before the initial render. It is also invoked every time <.link patch={...}> or push_patch/2 are used.
If I understand correctly, I can retrieve the requested path in handle_params/3, but to do so, I would need to add code to every live_view module. Is there no way to do this in the socket similar to a plug, where I don’t have to modify the controller?