1

This is the script tag I am supposed to include in head tag in NUXTjs

<script>(function (c, p, d, u, id, i) {
  id = ''; // Optional Custom ID for user in your system
  u = 'https://example.com/attribution_tracking/conversions/' + c + '.js?p=' + encodeURI(p) + '&e=' + id;
  i = document.createElement('script');
  i.type = 'application/javascript';
  i.async = true;
  i.src = u;
  d.getElementsByTagName('head')[0].appendChild(i);
}("6666", document.location.href, document));</script>

It is basically a self invoke function. I need help in how can I add to my head tag in NUXTjs file.

  • Does this answer your question? [How to add a 3rd party script code into Nuxt?](https://stackoverflow.com/questions/67534304/how-to-add-a-3rd-party-script-code-into-nuxt) – kissu Oct 12 '21 at 08:42
  • Take a look at the documentation, there's [an example](https://nuxtjs.org/docs/components-glossary/head/) of how they modify the head tag. It can be an object or a function that returns an object; and you feed it with the content you want to display. For your snippet however, if it has to stay unchanged, I suggest that you place it in a [middleware](https://nuxtjs.org/docs/components-glossary/middleware/). – gekkedev Oct 12 '21 at 08:53
  • @gekkedev have this in a middleware is probably not a good idea. – kissu Oct 12 '21 at 08:54
  • @kissu kindly let me know your reason. If you're loading external JS, having the middleware provides you with parts of the Nuxt context to better integrate it, right? – gekkedev Oct 12 '21 at 08:58
  • If you have a middleware with a `getElementsByTagName('head')[0].appendChild(i)`, it will add the script on each navigation. In less than 3 minutes, you will end up with the script running 30 instances on your SPA. @gekkedev – kissu Oct 12 '21 at 09:02

0 Answers0