Next.js Discord

Discord Forum

Best strategy for analytics

Unanswered
Large oak-apple gall posted this in #help-forum
Open in Discord
Large oak-apple gallOP
What is optimal way to load analytics script?

Inline in head? Inline in start of body? Inline in end of body?
Script component with beforeInteractive? With afterInteractive? With lazyOnload?

I understand that best way would be to bundle it, but it is most likely not possible in my case.

Original script loader provided by service uses async or defer to download main script from cdn, if it makes difference here.

5 Replies

American black bear
if you are using some analytics provider take a look at their docs
most load some kind of script in head or inject it later to dom using useEffect
the only thing of note here is to lazy load it since you want your initial request to take as little time as possible which means loading only stuff the user sees and in parallel if possible
@American black bear if you are using some analytics provider take a look at their docs
Large oak-apple gallOP
Their docs say literally "insert script into html closer to beginning so it can start faster, but better leave it to people who know what they do" 😀