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

How to get page title for analytics? #6

Closed
Kinzi opened this issue Feb 12, 2020 · 6 comments
Closed

How to get page title for analytics? #6

Kinzi opened this issue Feb 12, 2020 · 6 comments
Labels
enhancement New feature or request

Comments

@Kinzi
Copy link
Contributor

Kinzi commented Feb 12, 2020

I'm mainly using this for analytics stuff. Usually Analytics tracks the page title along with the url.

Anyway the page title can be pushed to the nuxtRoute data layer object on route change? This way I could simply set up a variable for it in GTM

@pi0 pi0 added the enhancement New feature or request label Feb 12, 2020
@pi0
Copy link
Member

pi0 commented Feb 12, 2020

Hi @Kinzi and thanks for idea :) Do you mind creating a PR to add support?

@Kinzi
Copy link
Contributor Author

Kinzi commented Feb 12, 2020

Sure: #8

Seems to work for me but I'm not an expert with this stuff and I'm sure there is a better solution. ;)

Had to set a timeout because otherwise it would fetch the last page title not the new one unfortunately...

@pi0
Copy link
Member

pi0 commented Feb 23, 2020

Released by 2.2.0: https://github.com/nuxt-community/gtm-module/releases/tag/v2.2.0

@pi0 pi0 closed this as completed Feb 23, 2020
@voltane
Copy link

voltane commented Apr 14, 2020

Sure: #8

Seems to work for me but I'm not an expert with this stuff and I'm sure there is a better solution. ;)

Had to set a timeout because otherwise it would fetch the last page title not the new one unfortunately...

I'm facing a problem with that timeout. 250ms is not enough for pages, where head() method is defined. In my case that timeout is around 500ms (otherwise still previous page title is send) and it seems that this value is use-case dependant. Anyway i think that simply increasing the timeout here is not the way to go.
The problem is that head() method is called in the same time or soon after vue-router afterChange event.
For now i dont have any workaround for that but maybe we can mess around with beforeRouteEnter -> next() callback which is called after DOM is updated ? Registering a global mixin with that callback defined and inside pushing the PageView event seems promising.

Edit:

I added this code and it works fine. If You guys want i can push it as a pull request.

@jakubm95
Copy link

jakubm95 commented Dec 1, 2020

Hello @pi0
Unfortunately it doesn't work properly, pageTtitle is being sent from the previous page

image

@djave-co
Copy link

Also facing @jakubm95 issue. The head() function using async data.

  async asyncData({ $axios, route, app }) {
    const page = await app.$api.$get(route.fullPath);
    return { page };
  },
  head() {
    let meta = this.page.meta;
    let titleMeta = meta.find((m) => m.property == "title");
    let title = titleMeta ? titleMeta.content : "Default title";
    return { title, meta };
  },

The first page load tracks the default title, then every subsequent page load tracks the previous page title.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants