Cookies management

Angular Bootstrap 5 Cookies management plugin

This component is used to save in the browser data that we want to have access at the next user visit. For example how many times or when last time user visit our page.

Cookies management built with the latest Bootstrap 5. Options included: show modals only to new users, set a cookie, and much more.

Note: Read the API tab to find all available options and advanced customization


Basic example

The Cookies Management Service provides methods to add, remove and get cookies


Advanced example

Show Modal for new users

Using the showModalNewUser method you can display the modal for new users. Click start button to start simulate this behavior. After next click modal don't show any more until you click reset.

        
            
          <button (click)="setCookieForNewUser()" type="button" class="btn btn-primary">
            Show Modal
          </button>
          <button (click)="resetCookieForNewUser()" type="button" class="btn btn-primary">
            Reset
          </button>
          
        
    
        
            
          import { Component } from '@angular/core';

          @Component({
            selector: 'app-root',
            templateUrl: './app.component.html',
          })
          export class AppComponent {
            modalRefNewUser: MdbModalRef<ModalNewUserComponent> | null = null;
          
            constructor(
              private _cookieService: MdbCookiesManagementService,
              private _modalService: MdbModalService
            ) {}
          
            setCookieForNewUser() {
              const isFirstVisitOnPage = !this._cookieService.get('is-first-visit');
              if (isFirstVisitOnPage) {
                this.showModalNewUser();
              }
            }
          
            resetCookieForNewUser() {
              this._cookieService.remove('is-first-visit');
            }
          
            showModalNewUser() {
                this.modalRefNewUser = this._modalService.open(ModalNewUserComponent);
                this.modalRefNewUser.onClose.subscribe((message: any) => {
                  this._cookieService.set('is-first-visit', false, { expires: 365 });
                });
            }
          }
          
          
        
    

Show modal after next visit

If you want to display the modal to the person who will visit the page again, you can use the showModalScoring method. Enter as a parameter the information after how many visits to the website the modal should appear. Click start button 3 times to test this feature.

        
            
            <button (click)="setCookieForScoring()" type="button" class="btn btn-primary">
              Show Modal
            </button>
            <button (click)="resetCookieForScoring()" type="button" class="btn btn-primary">
              Reset
            </button>
            
        
    
        
            
              import { Component } from '@angular/core';
  
              @Component({
                selector: 'app',
                templateUrl: './app.component.html',
              })
              export class AppComponent {
                  modalRefScoring: MdbModalRef<ModalScoringComponent> | null = null;
                
                  constructor(
                    private _cookieService: MdbCookiesManagementService,
                    private _modalService: MdbModalService
                  ) {}

                  setCookieForScoring() {
                    this.showModalScoring(3);
                  }
                
                  resetCookieForScoring() {
                    this._cookieService.remove('visit-counter');
                  }
                
                  showModalScoring(value) {
                    const counter = Number(this._cookieService.get('visit-counter')) || 0;
                    const newCounter = counter + 1;
                
                    this._cookieService.set('visit-counter', newCounter, { expires: 365 });
                
                    if (newCounter === value) {
                      this.modalRefScoring = this._modalService.open(ModalScoringComponent);
                    }
                  }
              }
            
        
    

Cookies management - API


Import

        
            
      import { MdbCookiesManagementService } from 'mdb-angular-cookies-management';
      …
      @NgModule ({
        ...
        providers: [MdbCookiesManagementService],
        ...
      })
      
        
    

Methods

Name Description
set(name, value, attributes) Set Cookie
get(name) Get Cookie
remove(name) Remove Cookie