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

[Question] How to have a custom page title sent to GA ? #8

Closed
mahesh-chotrani opened this issue Feb 12, 2021 · 4 comments
Closed

[Question] How to have a custom page title sent to GA ? #8

mahesh-chotrani opened this issue Feb 12, 2021 · 4 comments

Comments

@mahesh-chotrani
Copy link

By default, from GA documentation, the page title which is being sent to GA (on a page view) is document.title.

The below code to provide custom title works inconsistently.

import { useLocation } from 'react-router-dom';
import { useGA4React } from 'ga-4-react';
. . . 
      const location = useLocation();
      const ga4react = useGA4React('MEASURE_ID');
. . .
      ga4react.pageview(
        location.pathname + location.search,
        null,
        location.pathname, // because we do not have page title, use path instead. Can have custom logic
      );

The title being sent to GA is mostly empty, and sometimes, the page location is sent. Assigning it to a separate variable, just before pageview also doesn't helps.

Anything missed here ?

@unrealmanu
Copy link
Owner

Hello, the pageview method, is nothing more than an "alias" of https://developers.google.com/gtagjs/reference/event#page_view

have you tried to use it instead?

ga4react .gtag ('event', 'page_view', {
page_position: "https://example.com/about",
page_path: "/ about",
page_title: "Information"
});

let me know

@mahesh-chotrani
Copy link
Author

I tried with ga4react.gtag as well, but it didn't work. The only thing which worked was to set document.title. But that we cannot have in our project due to some other nomenclature for page titles.

Setting page title through GA methods dynamically did not work. Let me know, in case you can think of any other solution.

@unrealmanu
Copy link
Owner

unrealmanu commented Feb 14, 2021

For SPA-like sites, the google documentation indicates this solution:
https://developers.google.com/analytics/devguides/collection/gtagjs/single-page-applications
gtag('config', 'UA-1234567-89', {'page_path': '/new-page.html'});

but there is no mention of the title of the document.

Maybe try to configure google analytics as shown here:
https://developers.google.com/analytics/devguides/collection/gtagjs/pages

@mahesh-chotrani
Copy link
Author

Appreciate swift responses, @unrealmanu

But this also doesn't works. If two pages are navigated in an interval less than 2-3 secs, the last page viewed is sent to GA (which is expected), but with incorrect title.
Quickly navigated pages get skipped is fine (because they can be ignored anyways), but for whatever is being sent to GA, should have correct dynamic page title.

Let me know, in case any other approach. Thanks

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

2 participants