Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including @progressivewebcomponents/pwa-install with all npm packages installed. Try it out:

var pwaInstall = require("@progressivewebcomponents/pwa-install")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@progressivewebcomponents/pwa-install v0.1.0

Progressive web apps install (add to home screen) vanilla web component

Published on NPM Published on webcomponents.org

<pwa-install>

Progressive web apps install (add to home screen) vanilla web component.

Install

npm i @progressivewebcomponents/pwa-install

Install web components polyfills (needed for older browsers):

npm i -D @webcomponents/webcomponentsjs

Alternatively, you can use unpkg.

Import

Load web components polyfill (if needed):

<script src="../node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>

In JS files

import '../node_modules/@progressivewebcomponents/pwa-install/pwa-install.js';

In HTML files

ES Modules (recommended)

<script type="module" src="../node_modules/@progressivewebcomponents/pwa-install/pwa-install.js"></script>

HTML Imports (deprecated)

<link rel="import" href="../node_modules/@progressivewebcomponents/pwa-install/pwa-install.html">

getInstalledRelatedApps

https://developers.chrome.com/origintrials/#/view_trial/855683929200394241

chrome://flags/#enable-experimental-web-platform-features

Use

Polymer

<pwa-install
  id="a2hs"

  available="{{installAvailable}}"
  platforms="{{platforms}}"
  choice-result="{{choiceResult}}"
  supported="{{relatedAppsSupported}}"
  related-apps="{{relatedApps}}"

  on-pwa-install-available="handlePWAInstallAvailable"
  on-pwa-install-install="handlePWAInstall"
  on-pwa-install-installed="handlePWAInstalled"
  on-pwa-install-error="handlePWAInstallError"

  on-available-changed="handleAvailableChanged"
  on-platforms-changed="handlePlatformsChanged"
  on-choice-result-changed="handleChoiceResultChanged"
  on-supported-changed="handleSupportedChanged"
  on-related-apps-changed="handleRelatedAppsChanged">
</pwa-install>
const pwaInstall = this.$.a2hs;

pwaInstall.install();

pwaInstall.getInstalledRelatedApps();

LitHTML/LitElement

<pwa-install
  id="a2hs"

  @pwa-install-available="${this.handlePWAInstallAvailable}"
  @pwa-install-install="${this.handlePWAInstall}"
  @pwa-install-installed="${this.handlePWAInstalled}"
  @pwa-install-error="${this.handlePWAInstallError}"

  @available-changed="${this.handleAvailableChanged}"
  @platforms-changed="${this.handlePlatformsChanged}"
  @choice-result-changed="${this.handleChoiceResultChanged}"
  @supported-changed="${this.handleSupportedChanged}"
  @related-apps-changed="${this.handleRelatedAppsChanged}">
</pwa-install>
const pwaInstall = this.shadowRoot.getElementById('a2hs');

pwaInstall.install();

pwaInstall.getInstalledRelatedApps();

Vanilla (without any frameworks/libraries)

<pwa-install id="a2hs"></pwa-install>
const pwaInstall = document.getElementById('a2hs');

pwaInstall.addEventListener('pwa-install-available', handlePWAInstallAvailable);
pwaInstall.addEventListener('pwa-install-install', handlePWAInstallInstall);
pwaInstall.addEventListener('pwa-install-installed', handlePWAInstallInstalled);
pwaInstall.addEventListener('pwa-install-error', handlePWAInstallError);

pwaInstall.addEventListener('available-changed', handleAvailableChanged);
pwaInstall.addEventListener('platforms-changed', handlePlatformsChanged);
pwaInstall.addEventListener('choice-result-changed', handleChoiceResultChanged);
pwaInstall.addEventListener('supported-changed', handleSupportedChanged);
pwaInstall.addEventListener('related-apps-changed', handleRelatedAppsChanged);

pwaInstall.install();

pwaInstall.getInstalledRelatedApps();

Full live demo (source code)

Google Analytics

handlePWAInstallAvailable() {
  if (window.ga) {
    ga('send', 'event', 'pwa-install', 'available', this.platforms);
  }
}

handlePWAInstallInstall() {
  if (window.ga) {
    ga('send', 'event', 'pwa-install', this.choiceResult.outcome, this.choiceResult.platform);
  }
}

handlePWAInstallInstalled() {
  if (window.ga) {
    ga('send', 'event', 'pwa-install', 'installed', this.choiceResult.platform);
  }
}

handlePWAInstallError() {
  if (window.ga) {
    ga('send', 'event', 'pwa-install', 'error', this.choiceResult.platform);
  }
}

Further reading

Patterns for Promoting PWA Installation (mobile)

Detect if your Native app is installed from your web site

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free