LogoLogo
Sign inSign up
  • PushPushGo Documentation
  • Web push
    • Overview
    • Instructions
      • Subscription tests
    • Service worker
    • Integration script
      • Subscription form
        • Subscription form creator
          • Topics
        • Subscription form delay
        • Display form rules
        • Confirmation window
      • Bell widget
        • Inbox
        • Topics
      • Google Analytics & Google Tag Manager
      • Default notification
      • Beacons
    • SDK
      • Examples
    • JS Code
    • Safari support
    • FAQ
  • Mobile push
    • Overview
    • Google Android
    • Apple iOS
    • Huawei Android
  • ONSITE NOTIFICATIONS
    • Overview
  • Onsite notifications
    • Create onsite notification
    • Edit onsite notification
    • Onsite notification report
    • Inbox
  • Onsite notifications list
  • Subscribers
    • Subscribers
      • Activity status
      • Subscriber Details
    • Labels
      • System Labels
      • Add Labels Manually
    • Segments
      • Create Segment
      • Copy Segment
      • Update Segment
      • Delete Segment
    • Geolocation
  • Campaigns
    • Campaigns
    • Push campaign
      • Create Campaign
        • Content
        • Audience
          • By Segment
          • By Labels
        • Provider options
        • Time
        • Drafts
      • Campaign List
        • Cancel Campaign
        • Copy Campaign
        • Copying and pasting campaigns between projects
        • Delete Campaign
      • Campaign Report
    • AB test
      • Create AB test
        • Variants
        • Audience
        • Provider options
        • Time
        • Save draft or accept a test
      • AB test list
      • Winner selection
      • AB test report
      • Cancel AB Test
      • Send AB test saved as draft
      • Resend AB test
      • Delete AB test
    • RSS campaign
      • Create RSS campaign
      • Audience
      • Enable RSS campaign
      • RSS campaign report
    • Chrome plugin for Rocket push
    • Multi Push
    • Daily push capping
    • Planner
    • FAQ
  • Automation
    • Overview
    • Automation
      • Create automation
        • Name
        • Flow
        • Renew flow
        • Start date
        • End date
        • Building the flow
        • Step
          • Trigger
          • Campaign
            • Create campaign
            • Edit campaign
            • Campaign templates
      • Edit automation
        • Update automation
      • Report
        • Automation report
        • Campaign report
    • Automation list
      • Prority
      • Action
      • Status
      • Reset flow time
  • Analytics
    • Project analytics
      • Subscribers
      • Campaigns
      • Automation
    • Organization analytics
      • Organization dashboard
        • Subscribers
        • Campaigns
        • Automation
      • Project comparison
    • Exports
  • Integrations
    • Webhooks
    • REST API Reference
  • Release notes
Powered by GitBook
On this page
  • Custom subscribe button
  • Set Custom ID
  • Add subscriber tags
  • Send automation data

Was this helpful?

  1. Web push
  2. SDK

Examples

With the SDK client configured, except from basic subscription to web push notifications, you can use it to create your own component for subscribing to web push notifications, segment your subscribers with tags, link them with your CRM database by setting custom id, or send data for automation purposes.

Custom subscribe button

  1. Initialize SDK client

  2. Use it to add web push registry functionality to button elements

let client;

function getClient() {
  if (!client) {
    const config = {
      projectId: $projectId,
      isHTTPS: $isHTTPS,
      vapidPublicKey: $vapidPublicKey,
      endpoint: 'https://api.pushpushgo.com',
    };

    client = new ppg.sdk.Client(config);
  }
  return client;
}

function subscribeButtonClickedHandler(event) {
  event.preventDefault();
  client
    .register()
    .then(subscriberId => {
      setState(`user subscribed with id: ${subscriberId}`, false)
    });
}

function unsubscribeButtonClickedHandler(event) {
  event.preventDefault();
  client
    .unsubscribe()
    .then(() => {
      setState('user unsubscribed', true)
    });
}

function setState(status, canSubscribe) {
  const li = document.createElement('li');
  li.innerText = status;
  
  document.querySelector('#state').appendChild(li);
  document.querySelector('#unsubscribe').disabled = canSubscribe;
  document.querySelector('#subscribe').disabled = !canSubscribe;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Subscribe button demo</title>
    <script src="https://cdn.pushpushgo.com/scripts/sdk.js"></script>
    <script type="application/javascript" src="subscribe.js"></script>
</head>
<body>
  <ul id="state">
      <li>checking push notifications state...</li>
  </ul>
  <button id="subscribe" disabled="disabled">click to subscribe</button>
  <button id="unsubscribe" disabled="disabled">click to unsubscribe</button>
</body>
<script>
  window.addEventListener('load', function() {
    const unsubscribeButton = document.querySelector('#unsubscribe');
    const subscribeButton = document.querySelector('#subscribe');

    subscribeButton.addEventListener('click', subscribeButtonClickedHandler);
    unsubscribeButton.addEventListener('click', unsubscribeButtonClickedHandler);

    getClient()
      .isPushSupport()
      .then(() => client.isSubscribed())
      .then(() => client.getId())
      .then(subscriberId => {
        setState('push notifications are supported');
        if (!subscriberId) {
          setState('activate subscribe button', true);
        } else {
          setState('user already subscribed with id: ' + subscriberId, false);
        }
      })
      .catch((err) => {
        setState('ops, sth went wrong, check console')
        console.log(err)
        setState('push notifications are not supported')
      });

  });
</script>
</html>
importScripts('https://cdn.pushpushgo.com/$projectId/worker.js');

3. To test if the subscription was successful, you can send a transactional web push notification via API

  • subscriberId - from demo

curl --request POST \
  --url https://api.pushpushgo.com/project/$projectId/send/push \
  --header 'accept: application/json' \
  --header 'x-token: $x-token \
  --header 'content-type: application/json' \
  -d '{"to": $subscriberId, "message": {"title": "sdk example title", "body": "test sdk subscription example", "icon": "https://via.placeholder.com/150/150", "redirectLink": "https://www.google.com/"}}'

Set Custom ID

let client;

function getClient() {
  if (!client) {
    const config = {
      projectId: $projectId,
      isHTTPS: $isHTTPS,
      vapidPublicKey: $vapidPublicKey,
      endpoint: 'https://api.pushpushgo.com',
    };

    client = new ppg.sdk.Client(config);
  }
  return client;
}

function subscribe() {
  getClient()
    .isPushSupport()
    .then(() => client.isSubscribed())
    .then(isSubscribed => {
      if (!isSubscribed) {
        return client.register();
      } else {
        return client.getId();
      }
    })
    .catch(() => console.log('push is not supported'));
}

function setState(status) {
  const li = document.createElement('li');
  li.innerText = status;

  document.querySelector('#state').appendChild(li);
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Set customId demo</title>
    <script src="https://cdn.pushpushgo.com/scripts/sdk.js"></script>
    <script type="application/javascript" src="subscribe.js"></script>
</head>
<body>
  <div id="crmId">SomeIdentifier</div>
  
  <ul id="state">
      <li>checking push notifications state...</li>
  </ul>

</body>
<script>
  window.addEventListener('load', function() {
    const crmId = document.querySelector('#crmId').innerText;
      
    subscribe()
      .then(() => client
        .setCustomId(crmId)
        .then(() => setState('subscriber customId set to' + crmId))
        
      .catch((err) => {
        setState('ops, sth went wrong, check console')
        console.error(err)
      });
  });
</script>
</html>
importScripts('https://cdn.pushpushgo.com/$projectId/worker.js');

Add subscriber tags

let client;

function getClient() {
  if (!client) {
    const config = {
      projectId: $projectId,
      isHTTPS: $isHTTPS,
      vapidPublicKey: $vapidPublicKey,
      endpoint: 'https://api.pushpushgo.com',
    };

    client = new ppg.sdk.Client(config);
  }
  return client;
}

function subscribe() {
  getClient()
    .isPushSupport()
    .then(() => client.isSubscribed())
    .then(isSubscribed => {
      if (!isSubscribed) {
        return client.register();
      } else {
        return client.getId();
      }
    })
    .catch(() => console.log('push is not supported'));
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Add subscriber tags</title>
    <script src="https://cdn.pushpushgo.com/scripts/sdk.js"></script>
    <script type="application/javascript" src="subscribe.js"></script>
</head>
<body>
</body>
<script>
  window.addEventListener('load', function() {
    
    subscribe()
      .then(() => Promise.all([
        client.appendTags(['Demo tag', 'Other demo tag']),
        client.appendTags(['label:tag']),
      ]))
      .then(() => client.send());
  
  });
</script>
</html>
importScripts('https://cdn.pushpushgo.com/$projectId/worker.js');

Send automation data

let client;

function getClient() {
  if (!client) {
    const config = {
      projectId: $projectId,
      isHTTPS: $isHTTPS,
      vapidPublicKey: $vapidPublicKey,
      endpoint: 'https://api.pushpushgo.com',
    };

    client = new ppg.sdk.Client(config);
  }
  return client;
}

function subscribe() {
  getClient()
    .isPushSupport()
    .then(() => client.isSubscribed())
    .then(isSubscribed => {
      if (!isSubscribed) {
        return client.register();
      } else {
        return client.getId();
      }
    })
    .catch(() => console.log('push is not supported'));
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Set automation data</title>
    <script src="https://cdn.pushpushgo.com/scripts/sdk.js"></script>
    <script type="application/javascript" src="subscribe.js"></script>
</head>
<body>
</body>
<script>
  window.addEventListener('load', function() {
  
    subscribe()
      .then(() => Promise.all([
        client.setSelector('basket', 19),
        client.appendTags(['Some condition tag', 'Other demo tag']),
        client.addTagsToRemove(['label:tag']),
        client.setCustomId('SomeIdentifier')
      ]))
    .then(() => client.send());
    
  });
</script>
</html>
importScripts('https://cdn.pushpushgo.com/$projectId/worker.js');

Last updated 3 months ago

Was this helpful?

x-token - generate in the application

You can link web push subscriberId with your own identifier by setting the customId with

To be notified when customId is set, you can integrate with a subscriber:update

To add tags to subscriber, use

When you have automation configured in the you will need to feed it with data, by sending a beacon by . Assume you have a selector with a name basket defined in the application, and you need to set it's value for the automation. In addition lets operate on tags that might be used in automation conditions and set the Custom ID.

access keys section
Webhook
client.setCustomId()
client.appendTags()
app.pushpushgo,
client.send()