ngx-cookieconsent icon indicating copy to clipboard operation
ngx-cookieconsent copied to clipboard

How do I opt-in a session when cookie consent is not required?

Open bacongobbler opened this issue 9 months ago • 0 comments

Is your feature request related to a problem? Please describe

I do not know how to check whether the user is in a region where cookie consent is not required. I want users who are in a region where cookie consent is not required (USA) to opt-in by default, and users who are in a region where cookie consent is required (EU) to opt-out by default.

Currently, I have my cookie consent config set to something like this:

const cookieConfig: NgcCookieConsentConfig = {
  cookie: {
    domain: window.location.hostname,
  },
  type: 'opt-in',
  location: true,
};

And currently I have my app.component.ts set up like this:

import { Component, OnInit, OnDestroy } from '@angular/core';
import { NgcCookieConsentService } from 'ngx-cookieconsent';
import { Subscription }   from 'rxjs';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit, OnDestroy {

  //keep refs to subscriptions to be able to unsubscribe later
  private statusChangeSubscription!: Subscription;

  constructor(private ccService: NgcCookieConsentService){}

  ngOnInit() {

    this.statusChangeSubscription = this.ccService.statusChange$.subscribe(
      (event: NgcStatusChangeEvent) => {
        switch (event.status) {
          case 'allow':
            // when the user consents, we start collecting analytics.
            doSomething();
            break;
          case 'deny':
            // when the user declines, we stop collecting analytics.
            doSomething();
            break;
          default:
            // when the user dismisses the window, do nothing
            break;
  }

  ngOnDestroy() {
    // unsubscribe to cookieconsent observables to prevent memory leaks
    this.statusChangeSubscription.unsubscribe();
  }
}

With this logic, if the user is in a region like the US, they never "allowed" cookies, so we never start collecting analytics from the user.

Describe the idea you'd like

If the user is in a region where cookie consent law is not required, I'd like to disable the cookie consent component and opt-in the user to cookie consent. However if the user is in a region where cookie consent law is required, I'd like to enable the cookie consent component and provide them with the choice to allow or deny cookies (type: "opt-in").

Describe alternatives you've considered

Our current system does not use the location feature, which works around this issue by forcing every user to consent.

Additional context

No response

bacongobbler avatar Sep 13 '23 18:09 bacongobbler