Skip to content

useHead() was called without provide context, ensure you call it through the setup() function #437

@dvogelg4al

Description

@dvogelg4al

🐛 The bug

Updated Nuxt and all modules to the latest and getting the error in the title. Worked without issues before.

Here is the error stack:

GAnalytics.ts:20 Error: useHead() was called without provide context, ensure you call it through the setup() function.
    at injectHead (vue.BYLJNEcq.mjs?v=1c7113e8:26:9)
    at useScript (vue.cHBs6zvy.mjs?v=1c7113e8:34:33)
    at useScript (useScript.js?v=1c7113e8:30:20)
    at useRegistryScript (utils.js?v=1c7113e8:48:10)
    at useScriptGoogleAnalytics (google-analytics.js?v=1c7113e8:9:10)
    at Object.trackEvent (GAnalytics.ts:4:25)

I use this composable for firing GA events:

export const useGAnalytics = () => {
  const trackEvent = (eventName: string, params: any = null) => {
    try {
      const { proxy } = useScriptGoogleAnalytics();

      proxy.gtag("event", eventName, params);
    } catch (error) {
      console.error(error);
    }
  };
  return {
    trackEvent,
  };
};

I simply call trackEvent in various places, script tags, on event listeners on a button, etc. Worked fine before.

The bug can be seen in the reproduction. If you try to navigate between pages, the error occurs

🛠️ To reproduce

https://stackblitz.com/edit/github-cabjusrz?file=pages%2Fabout.vue,composables%2FGA.ts

🌈 Expected behavior

Expected events to fire without throwing an error.

ℹ️ Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions