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

Analytics Consent not sent on update #8210

Comments

@megamisan
Copy link

Operating System

Windows 11 23H2

Browser Version

Edge 124.0.2478.51

Firebase SDK Version

10.11.0

Firebase SDK Product:

Analytics, AppCheck, Auth, Firestore, Functions, Storage

Describe your project's tooling

Vue.js app.

Describe the problem

I'm trying to get analytics to work with consent banner and sending advanced consent information with the setConsent method.

Steps and code to reproduce issue

In the projet, initialize Firebase first. I consider app to be a constant containing the firebase application instance in the following snippet:

import { setAnalyticsCollectionEnabled, setConsent } from 'firebase/analytics'

const analytics = app.analytics()
// Add some delay to wait for gtag loading
setTimeout(() => {
  console.log('Called')
  setAnalyticsCollectionEnabled(this._analytics, consent)
  setConsent({
    ad_personalization: 'denied',
    ad_storage: 'denied',
    ad_user_data: 'denied',
    analytics_storage: 'granted',
    functionality_storage: 'granted',
    personalization_storage: 'denied',
    security_storage: 'granted',
  })}, 5000)

Using Tag Assitant, I can see the tag events. However, I get this result:
image

The granted value is missing and there is strange numbered items.

Doing a some step-by-step debugging, I found the call to gtag. The third parameter which should be the ConsentSettings (see Set up consent mode on websites is set to the second parameter update instead of the third.
Debug screenshot

The setConsent method code is here packages/analytics/src/api.ts#L768.
It trigger the call to the gtagWrapper function with this line:

wrappedGtagFunction(GtagCommand.CONSENT, 'update', consentSettings);

The wrappedGtagFunction is an instance of the gtagWrapper function found in packages/analytics/src/helpers.ts#L279.

When the method is called before initialization of gtag, the call is made correctly. setContent sends the settings to _setConsentDefaultForInit and the packages/analytics/src/initialize-analytics.t:_initializeAnalyticss#L129 function use these settings correctly.

I see two possible fixes:

  • Removing the 'update' parameter in setConsent, thus the diff:
    @@ -766,5 +766,5 @@
      // Check if reference to existing gtag function on window object exists
      if (wrappedGtagFunction) {
    -    wrappedGtagFunction(GtagCommand.CONSENT, 'update', consentSettings);
    +    wrappedGtagFunction(GtagCommand.CONSENT, consentSettings);
      } else {
        _setConsentDefaultForInit(consentSettings);
  • Retrieves the second argument from the args array.

I recommend the first option.

Original commits:

@megamisan megamisan added new A new issue that hasn't be categoirzed as question, bug or feature request question labels Apr 27, 2024
@google-oss-bot
Copy link
Contributor

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@megamisan megamisan changed the title Analytics Consent not sent Apr 27, 2024
megamisan added a commit to megamisan/firebase-js-sdk that referenced this issue Apr 27, 2024
* Fix call to consent update after integrating rest operator for gtag wrapper.
@jbalidiong jbalidiong added needs-attention api: analytics and removed needs-triage new A new issue that hasn't be categoirzed as question, bug or feature request labels Apr 29, 2024
@jbalidiong
Copy link
Contributor

@megamisan, thanks for reaching out to us. I was able to replicate the behavior you've mentioned. Also, thanks for sending a PR for a possible fix. I'll raise this to our engineering team or bring someone here that can provide more context about it. I’ll update this thread if I have any information to share.

@vytautas-vitkus-tg
Copy link

Can confirm the same issue on our site:
image

DellaBitta added a commit that referenced this issue May 15, 2024
We wrap the gtag function internally and pass setConsent calls through the wrapper function, which in turn calls the gtag SDK. While we invoke the wrapped function with parameters identical to those listed in the gtag functions, we were dropping the update string on the floor inside the wrapped function. Additionally, we were feeding the consentSettings as the second parameter to gag, which should have been the update string parameter.

This ended up clobbering the data, and not send update commands properly.

This change fixes our wrappedGtag func impl to properly pass update to gtag, along with the provided ConsentSettings argument.

Closes #8210
@DellaBitta
Copy link
Contributor

This fix should be in our next release. If you find that there's still an issue, or a new one, then please open a new support issue and feel free to link to this issue for context. Thanks!

@firebase firebase locked and limited conversation to collaborators Jun 15, 2024
tom-andersen pushed a commit that referenced this issue Jul 24, 2024
We wrap the gtag function internally and pass setConsent calls through the wrapper function, which in turn calls the gtag SDK. While we invoke the wrapped function with parameters identical to those listed in the gtag functions, we were dropping the update string on the floor inside the wrapped function. Additionally, we were feeding the consentSettings as the second parameter to gag, which should have been the update string parameter.

This ended up clobbering the data, and not send update commands properly.

This change fixes our wrappedGtag func impl to properly pass update to gtag, along with the provided ConsentSettings argument.

Closes #8210
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.