UaSDK

UaSDK

The main Web SDK object. It can not be instantiated manually. It is returned by the async loader.

Constructor

new UaSDK()

Properties:
Name Type Attributes Description
sdk.channel UaSDK.Channel <nullable>

The channel object for the current browser if registered.

sdk.contact Promise.<UaSDK.Contact>

The contact object for the current browser.

Note: Contacts are not currently enabled by default. The contact Promise will reject with an error if contacts are not enabled in your SDK.

sdk.preferenceCenters PreferenceCenterManager

interface for retrieving preference center definitions.

sdk.featureFlags FeatureFlagManager

interface for determining feature flag eligibility

sdk.localeOverride UaSDK.LocaleOverride <nullable>

The LocaleOverride object, when set it overrides the browser locale information

sdk.CustomEvent CustomEvent

The constructor to make custom events.

sdk.canRegister boolean

This is true if the browser isSupported and the page is secure and Notifications have not been blocked at the browser level

sdk.isSupported boolean

This is true if the browser has web push abilities or if config flag loadIfPushNotSupported is set to true

sdk.isWebPushSupported boolean

This is true if the browser has web push abilities.

sdk.permission string <nullable>

The browser permission state to display notifications.

Secure contexts return 'granted', 'denied', or 'default'.

Insecure contexts always return undefined.

Example
// You will always use this through the async loader
UA.then(function(sdk) {// use the sdk here})

Classes

Channel
Contact
LocaleOverride
Plugins

Members

dataCollectionEnabled :Promise.<boolean>

Enable data collection. Resolves to a boolean that is true when data collection is enabled.

Type:
  • Promise.<boolean>
Example
sdk.dataCollectionEnabled = true // setter
sdk.dataCollectionEnabled.then(enabled => enabled === true) // getter is asynchronous

disableAnalytics :Promise.<boolean>

Disables analytics for this browser. Resolves to a boolean that is true if analytics are disabled.

Type:
  • Promise.<boolean>
Example
sdk.disableAnalytics = true // setter
sdk.disableAnalytics.then(disabled => disabled === true) // getter is asynchronous

Methods

(async) create() → {Promise.<UaSDK.Channel>}

Create a channel for this browser, enabling the SDK and making the browser known to Airship but without prompting them to opt in for push; this will:

  • Collect browser information for out of the box tag segmentation.
  • Register with Urban Airship and resolve the returned channel object.

This method is similar to UaSDK#register, except that it will:

  • Not prompt the user to allow notifications
  • Create an opted-out channel

If you wish to emit events about this browser, or associate it with a user in Airship, but without requiring opt in to web push notifications, you may use this method. You may later call the UaSDK#register method should the browser opt in to web push notifications.

Note: for compatibility with existing installations, opted out browsers will not emit events to Airship by default. You may change this behavior by passing trackEventsForOptOutChannels: true in your SDK initialization.

If you have already called register() for this browser, there is no need to call this method.

Rejects with an error if registration fails, or if the browser is not a secure context. See UaSDK#canRegister.

Fires:
Returns:
Type:
Promise.<UaSDK.Channel>

getChannel()

Return the channel for the current user as soon as it is created (new registration) or loaded (existing registration).

Example
UA.then(sdk => {
 sdk.getChannel().then(channel => {
   console.log("Channel ID: %s", channel.id)
 })
})

(async) register() → {Promise.<UaSDK.Channel>}

Register the current browser with Airship, creating an opted in channel for the browser after prompting the user for permission if necessary; this will:

  • Fetch the browser's subscription object, prompting the user for permission if necessary.
  • Collect browser information for out of the box tag segmentation.
  • Register with Urban Airship
  • Opt a channel back in to push if it has opted out.

Rejects with an error if registration fails, or if the browser is not a secure context. See UaSDK#canRegister.

You are responsible for providing a UI to register a user.

<button id="register">Sign up for notifications</button>
<script>
document.getElementByID('register').addEventListener('click', function () {
    return UA.then(sdk => sdk.register())
})
</script>

Should you wish to register the browser with Airship but not prompt for notification opt-in, see the UaSDK#create method.

Fires:
Returns:
Type:
Promise.<UaSDK.Channel>

Events

channel

The main SDK object fires a channel event when a channel is registered, loaded or changed.

Properties:
Name Type Description
ev.detail Channel

The channel that changed

ev.channel Channel

The channel that changed (Deprecated, please use ev.detail)

Example
// NOTE: if you intend to set attributes, tags, named user, or anything that could modify
// the channel within your event listener, make sure to set the `once` option to `true`
// in order to prevent an infinite loop.

  sdk.addEventListener('channel', ev => {
    ev.detail === sdk.channel
  }, { once: true })

push

If you are on an https page that is in-scope of your push-worker.js: The main SDK object fires a push event when the browser receives a push.

Properties:
Name Type Description
ev.push object

the push payload

Example
sdk.addEventListener('push', ev => {
    // ev.push is the push payload object
  })

tags

The main SDK object fires a tags event any time the tags change.

Properties:
Name Type Description
ev.detail TagList

The channel's tags list

ev.tags TagList

The channel's tags list (Deprecated, please use ev.detail)

Example
sdk.addEventListener('tags', ev => {
    ev.detail === sdk.channel.tags.list
  })