Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event ($gtm) in middleware #154

Open
oscarhandsome opened this issue Jun 21, 2022 · 0 comments
Open

Event ($gtm) in middleware #154

oscarhandsome opened this issue Jun 21, 2022 · 0 comments

Comments

@oscarhandsome
Copy link

Hi everyone! Thanks to owners for this module.
It very helps and make development faster!

I have question. Because my event not happens.
I want to detect what pages are not available and send event to GA.

Correct that $gtm not available on ssr stage? Might be better to use another structure?

middleware/navigation.js

export default function ({ app, route, req }) {
  if (route.name === 'not-found') {
    const domain = process.client ? window.origin : req.headers.host
    const data = {
      event: 'page_not_available',
      url: domain + route.path,
    }
    app.$gtm.push(data)
  }
}

nuxt.config.js

 router: {
    middleware: 'navigation',
    ...
    }
}

into my _slug.vue

async asyncData({ app, store, params, redirect, route,
  }: { app: any, store: any, params: any, redirect: any, route: any  }): Promise<any> {
    try {
      const response = await store.dispatch('....myvuex', {
        slug: params.slug,
      })
      if (response.status_code === 400) {
        throw new Error(`${response.status_code}`)
      }
    } catch (error) {
      redirect('/404')
      // redirect({ name: 'not-found', params: { slug: route.path } })
    }
  },
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant