Skip to content

FR: Disable automatic page views in analytics #3988

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

Closed
jasontproject opened this issue Oct 23, 2020 · 9 comments
Closed

FR: Disable automatic page views in analytics #3988

jasontproject opened this issue Oct 23, 2020 · 9 comments

Comments

@jasontproject
Copy link

[REQUIRED] Describe your environment

  • Operating System version: MacOS 10.15.7
  • Browser version: Chrome 86.0.4240.80
  • Firebase SDK version: 7.24.0
  • Firebase Product: analytics

[REQUIRED] Describe the problem

I would like to be able to disable the default Google Analytics page_view measurement when using the firebase JS SDK. This can be done when using GA on its own (https://developers.google.com/analytics/devguides/collection/ga4/disable-page-view).
However when calling firebase.analytics(), which (among other things) causes the page view event to be fired, there is no option to prevent the default page_view measurement.

Steps to reproduce:

Create a firebase project and a web app within. Follow the instructions to install the firebase web app config on a simple web site (a local web server with a single html page is all you need). This will include adding a couple script tags to your page, including one that looks something like this:

var firebaseConfig = {...};
firebase.initializeApp(firebaseConfig);
firebase.analytics();

Relevant Code:

That last call to firebase.analytics() involves a call to initializeIds which (I think) causes a page_view event to be logged.
If there was an option to have send_page_view:false be added to the config options in there, it would be nice.

In the meantime, here is the workaround I am attempting. I am placing the following ABOVE the firebase initialization scripts:

<script src="https://www.googletagmanager.com/gtag/js?l=dataLayer"></script>
<script>
window.dataLayer = window.dataLayer || [];

function gtag() { dataLayer.push(arguments); }

gtag('js', new Date());

gtag('config', 'MY_FB_APP_MEASUREMENT_ID', { 'send_page_view': false });
</script>

This script is based on the snippet GA gives you to when you create a Web Stream (Web Stream Details > Tagging Instructions > Add new on-page tag > Global site tag) combined with instructions to use Firebase with existing gtag.js tagging (https://firebase.google.com/docs/analytics/get-started?platform=web#firebase-gtag).

It seems to prevent the default page_view as I desire.

I am not sure how sustainable this is though, since the "dataLayer" variable is inside the firebase library. It could change at some point maybe?

@hsubox76
Copy link
Contributor

The value of the dataLayer variable only changes if the user specifies a custom dataLayer name using firebase.analytics.settings() so you don't need to be concerned about that part.

I am not 100% sure if calling a config on this measurement ID before Firebase does it will interfere with Firebase's ability to associate gtag events with your Firebase project. I don't think it should, but double check you're still getting events in the Firebase dashboard as you expect.

If you don't want any page_view events fired on this page, it might be safer to use gtag('set', { 'send_page_view': false }) which I think should set the property globally for all measurement IDs.

@jasontproject
Copy link
Author

Thanks @hsubox76, based on your response my workaround is simplified to this snippet placed above the calls to firebase initializeApp() and analytics():

<script>
window.dataLayer = window.dataLayer || [];
function gtag() {dataLayer.push(arguments);}
gtag('set', { 'send_page_view': false });
</script>

This seems to work as expected, no page views are sent.

@aaronhung215
Copy link

Will this issue be resolved in the future?
Otherwise, I need to develop both gtag & firebase method in my webview on APP...
I think the simplest way is to use firebase method to disable or override the default event parameter value.

@hsubox76
Copy link
Contributor

I think it might be a good idea to allow users to set custom config options on init, perhaps using the firebase.analytics.settings() method. We need to run this proposal by the rest of the team and get approval so it may take some time, and that may not end up being the exact solution decided on.

@aaronhung215
Copy link

The further problem I encounter, if I use the 'gtag' method on my APP, it's will be a potential security problem under high-standard security requirements. I think to use firebase method of sdk is the way to resolve fundamentally.

@hsubox76
Copy link
Contributor

I'm working on a method to allow setting gtag config settings such as { 'send_page_view': false } at initialization of analytics, which will be available in the upcoming modularized version of Firebase.

But can I ask why using gtag is a security problem? Firebase Analytics is just wrapping gtag under the hood so if you're using Firebase Analytics you're already using it.

@nicoqh
Copy link

nicoqh commented Mar 7, 2021

Some additional observations:

In addition to logging page views when the gtag is first initialized, a standard GA4 setup will log page view events from history state changes (it's one of the default "enhanced measurements"). So, for many SPAs, the initial gtag("config"...) is enough.

But when using the Firebase library, history changes don't trigger page view events. Is this because the history change event is disabled for Firebase projects? Viewing and editing data stream settings from the GA console is disabled if it's connected to Firebase. It would be very helpful if this was documented.

We can't use the built-in history change trigger, nor disable the first page view event.

@aaronhung215
Copy link

I'm working on a method to allow setting gtag config settings such as { 'send_page_view': false } at initialization of analytics, which will be available in the upcoming modularized version of Firebase.

But can I ask why using gtag is a security problem? Firebase Analytics is just wrapping gtag under the hood so if you're using Firebase Analytics you're already using it.

Yes,your opinion is right. But under may company security policy, it become a contradiction problem. You can skip it.

@hsubox76
Copy link
Contributor

hsubox76 commented Feb 3, 2022

We've added the ability to set a config on initialization of analytics in v9 as part of the settings object, see https://firebase.google.com/docs/reference/js/analytics.md#initializeanalytics

@hsubox76 hsubox76 closed this as completed Feb 3, 2022
@firebase firebase locked and limited conversation to collaborators Mar 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants