PrivatusAnalytics.Plug (Privatus Analytics v1.0.0)

Copy Markdown View Source

Adds the script tag just before </head> in HTML responses.

# lib/my_app_web/router.ex
pipeline :browser do
  # ...
  plug PrivatusAnalytics.Plug
end

Adding the plug is the switch. It changes only responses with status 200, a text/html content type and no content-encoding header. It skips Ajax requests (x-requested-with: XMLHttpRequest), pages without a </head>, and pages that already have the tag. It fixes the content-length header.

Options are the same as for PrivatusAnalytics.tag/1, for example plug PrivatusAnalytics.Plug, modules: ["engage", "auto"]. The nonce is read from conn.assigns.csp_nonce (change the name with nonce_assign:).