Profile import on push subscription
When a user subscribes to Web push notifications, a new profile will be added to the Databases and the Resource. The profile will get a subscription to the resource in which the Toolkit hosted on the site was generated.
When a subscriber opts in for push notifications from a browser used previously to open your emails, a profile will be matched and no new profile will be created.
When a subscriber clicks on a push notification, Altcraft Platform collects profile data:
- country, region, time zone, city, postal code and IP address from which the push subscription occurred;
- browser and operating system of the subscriber.
Mobile browsers support service worker and web push as well.
Custom business data on push subscription
When a new push subscriber is imported, you can import some business data as well. This can be any information you retrieve from the web session: pages visited, age, name or surname.
You can even search your databases for duplicate data before the import — to make sure it is a totally new customer.
Include these arguments into subscription initiation function:
- match — customer profile search to prevent duplication. The form is
{key: 'value'}
- update — data to add to a new or existing customer profile. The form is
{key1: 'value1', key2: 'value2', key3: 'value3'}
Here is an example with search by emailrecording name and surname:
akPush.initSubscription(
{
email: 'example@email.com'
}, // Search by email. If no matches found it will be recorded as a new profile's email.
{
_fname: 'Anatoly',
_lname: 'Wasserman'
} // subscriber's name and surname
)
If several databases are attached to the resource, you can specify the ID of the database into which you want to import a profile. An example with searching by email in a database with id=1:
akPush.initSubscription(
{
db: 1
email: 'example@email.com'
}, // Search by email. If no matches found it will be recorded as a new profile's email.
{
_fname: 'Anatoly',
_lname: 'Wasserman'
} // subscriber's name and surname
)
An example with searching by email in databases with id=15 and id=20:
akPush.initSubscription(
{
db: [15,20]
email: 'example@email.com'
}, // Search by email. If no matches found it will be recorded as a new profile's email.
{
_fname: 'Anatoly',
_lname: 'Wasserman'
} // subscriber's name and surname
)
You can find customer profile data field reference there.
Updating profile data and suspending subscriptions
akPush.updateSubscription
will return an error if the current token has not been received previously via akPush.initSubscription
, or it has been removed from the cache via akPush.removeToken
.
A user who subscribes to push notifications in the browser can change his state. For example, log in to your personal account and get a different match. At the same time, the current push subscription must be disabled on the previous profile and enabled on the new one. For these purposes, you must use the akPush.updateSubscription
method:
akPush.updateSubscription(
{
db: [15,20]
email: 'example@email.com'
},
{
_fname: 'Anatoly',
_lname: 'Wasserman'
} // subscriber's name and surname
inexclusive: false // whether or not to suspend all subscriptions to a resource with the current browser token for other profiles (by default)
)
If the inexclusive flag is not passed or is false:
- If the database filter is not specified, each resource database will contain only one profile with one subscription to the current token with the status "Subscribed".
- If the filter is specified, in each of the specified databases there will be only one profile with one subscription to the current token with the status "Subscribed". In other databases, all matching subscriptions to the resource for all profiles will be suspended.
If the inexclusive flag is set to true, then the call is equivalent to calling akPush.initSubscription
. It will update the profile with data and change the subscription status to "Subscribed".
Events for creating and updating a web push subscription
Platform users can subscribe to the following events:
- subscribe — creating a push subscription
- update_subscription — updating a subscription (subscriber token change)
When an event occurs, subscription data can be sent to a third-party server using an additional code.
To subscribe to events, add the following code to your website pages:
AKPush.events.on('subscribe', (data) => { // subscribe to the "subscribe" event
console.log(data);
});
AKPush.events.on('update_subscription', (data) => { // subscribe to the "update_subscription" event
console.log(data);
});
This code will output the subscriber token to the console, as well as information about the browser and push provider:
{
browser: "Firefox"
provider: "FirefoxFirebase"
token: "<subscriber token>"
}
When a user subscribes, the token is cached. You can reset it with the method:
AKPush.removeToken()
In case a token becomes outdated or a user re-subscribes to push, the platform will automatically update his subscription and issue a new token. This way no duplicate profiles will be created in the database.