Skip to Content
Menu

pwa()

Handles event listeners related to installing the service worker as a Progressive Web App to the user’s device.

JavaScript February 8, 2021

Usage

JavaScript
nebula.pwa()

Parameters

This function does not accept any parameters. Is this incorrect?

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /assets/js/modules/optimization.js on line 278.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    nebula.pwa = function(){
        let installPromptEvent; //Scope it to this level
    
        //Listen for ability to show SW install prompt
        window.addEventListener('beforeinstallprompt', function(event){
            event.preventDefault(); //Prevent Chrome <= 67 from automatically showing the prompt
            installPromptEvent = event; //Stash the event so it can be triggered later.
            jQuery('.nebula-sw-install-button').removeClass('inactive').addClass('ready'); //Show the Nebula install button if it is present.
        });
    
        //Trigger the SW install prompt and handle user choice
        nebula.dom.document.on('click', '.nebula-sw-install-button', function(){
            if ( typeof installPromptEvent !== 'undefined' ){ //If the install event has been stashed for manual trigger
                jQuery('.nebula-sw-install-button').removeClass('ready').addClass('prompted');
    
                installPromptEvent.prompt(); //Show the modal add to home screen dialog
    
                let thisEvent = {
                    category: 'Progressive Web App',
                    action: 'Install Prompt Shown',
                    label: 'The PWA install prompt was shown to the user',
                };
    
                nebula.dom.document.trigger('nebula_event', thisEvent);
                ga('send', 'event', thisEvent.category, thisEvent.action, thisEvent.label);
    
                //Wait for the user to respond to the prompt
                installPromptEvent.userChoice.then(function(result){
                    jQuery('.nebula-sw-install-button').removeClass('prompted').addClass('ready');
    
                    let thisEvent = {
                        category: 'Progressive Web App',
                        action: 'Install Prompt User Choice',
                        result: result,
                        outcome: result.outcome,
                    };
    
                    nebula.dom.document.trigger('nebula_event', thisEvent);
                    ga('send', 'event', thisEvent.category, thisEvent.action, thisEvent.outcome);
                    nebula.crm('event', 'Install Prompt ' + thisEvent.outcome);
                });
            } else {
                jQuery('.nebula-sw-install-button').removeClass('ready').addClass('inactive');
            }
    
            return false;
        });
    
        //PWA installed to the device
        window.addEventListener('appinstalled', function(){
            jQuery('.nebula-sw-install-button').removeClass('ready').addClass('success');
    
            let thisEvent = {
                category: 'Progressive Web App',
                action: 'App Installed',
                label: 'The PWA has been installed',
            };
    
            nebula.dom.document.trigger('nebula_event', thisEvent);
            ga('send', 'event', thisEvent.category, thisEvent.action, thisEvent.label);
        });
    };
    

    Override

    To override or disable this JavaScript function, simply redeclare it with the exact same function name.

    JavaScript
    nebula.pwa = function(){
        //Write your own code here, leave it blank, or return false.
    }