Website SDK API
JavaScript Async
The example assumes that you have the following code placed defined before calling Gravitec functions.
Update APP_KEY with your Gravitec AppId.
<script src="https://cdn.gravitec.net/storage/APP_KEY/client.js" async></script>
<script>var Gravitec= Gravitec || [];</script>
List of Functions
init
This is the only required method that you need to call for setting up Gravitec to receive push notifications. Call it from each page of your site.
- Parameters
- JSON options
- Boolean autoRegister (Optional) - Automatically show browser prompt to accept notifications. You can pass in "false" to delay this pop-up and then call registerUserForPush to prompt them later.
- Boolean createButton (Optional) It creates a default button that generates a window for receipt of the notifications, which appears after clicking.
- String tooltipText (Optional, use only with createButton) - Default: "One click subscription to our newsletter!" Set the text that will be shown to users on a default button.
Example:
var Gravitec = Gravitec || [];
Gravitec.push(["init", {"autoRegister":false}]);
registerUserForPush
Call it when you want to prompt the user to accept push notifications. Only call if you set "false" in autoRegister: when called "init".
Example:
var Gravitec = Gravitec || [];
Gravitec.push(["registerUserForPush", callback]);
segmentation.addTag
The method tags a subscriber basing on on app event of your choice, so that later you will be able to create segments to target users with the tag. It is recommended to use addTags rather than addTag if you need to add more than one tag in one operation.
Parameters:
- string value - set this to addTag.
- string value - value to set – tag name.
- Call back - set here call back to be fired when tag was successfully added and response from our server has been returned.
- Call back - set here call back to be fired, when tag was not added or error.
When error is encountered, err object is returned:
{
error:true,
message: 'something went wrong'
}
Example 1:
First call back is called when tag has been successfully added, second call back is called if some error has occurred during a tag addition:
var Gravitec = Gravitec || [];
Gravitec.push([
"segmentation.addTag","Tag1",
function() {console.log("Tag has been added")},
function(err) console.log(err.message)
}])
Example 2:
Call back is called when tag has been successfully added:
var Gravitec = Gravitec || [];
Gravitec.push([
"segmentation.addTag", "Tag1",
function() {console.log("Tag has been added")
}])
Example 3:
Without call back functions:
var Gravitec = Gravitec || [];
Gravitec.push([
"segmentation.addTag", "Tag1"
])
segmentation.addTags
Add provided tag(s) to subscriber’s tags. Thus, a push segment based on these tags could be created.
Parameters:
- string value - set this to addTags.
- Array of strings - JSON array of string value(s) – tag(s) to add.
- Call back - set here call back to be fired when tag was successfully added and response from our server has been returned.
- Call back - set here call back to be fired when tag was not added or error occured
When error is encountered, err object is returned:
{
error:true,
message: 'something went wrong'
}
Example 1:
First call back is called if tags have been successfully added, second call back is called if error has occurred during tags addition:
var Gravitec = Gravitec || [];
Gravitec.push([
"segmentation.addTags", [“new”,”new1”,”new2”],
function() {console.log("Tags has been added")},
function(err){console.log(err.message)
}])
Example 2:
Call back is called if tags have been successfully added:
var Gravitec = Gravitec || [];
Gravitec.push([
"segmentation.addTags",[“new”,”new1”,”new2”],
function() {console.log("Tags has been added")
}])
Example 3:
Without call back functions:
var Gravitec = Gravitec || [];
Gravitec.push([
"segmentation.addTags", [“new”,”new1”,”new2”]
])
segmentation.setTags
Clear all existing user tags and set new tags for the user.
Parameters:
- string value - set this to setTags.
- Array of strings - JSON array of string value(s) – tag(s) to set.
- Call back - set here call back that to be fired when tag was successfully added and response from our server has been returned.
- Call back - set here call back to be fired when tag was not added or error occured.
When error is encountered, err object is returned:
{
error:true,
message: 'something went wrong'
}
Example 1:
First call back is called if tags have been successfully set, second call back is called if error has occurred during tags setting:
var Gravitec = Gravitec || [];
Gravitec.push([
"segmentation.setTags", ["value1", "value2"],
function() {console.log("Tags has been set")},
function(err){console.log(err.message)
}])
Example 2:
Call back is called if tags have been successfully set:
var Gravitec = Gravitec || [];
Gravitec.push([
"segmentation.setTags",["value1", "value2"],
function() {console.log("Tags has been set")
}])
Example 3:
Without call back functions:
var Gravitec = Gravitec || [];
Gravitec.push([
"segmentation.setTags", ["value1", "value2"]
])
segmentation.removeTag
The method deletes a provided tag that was previously set for a user with addTag, addTags or setTags. Use removeAllTags if you need to delete all user’s tags.
Parameters:
- string value - set this to removeTag.
- string value - tag name to be removed.
- Call back - set here call back that to be fired when tag was successfully added and response from our server has been returned.
- Call back - set here call back that to be fired, when tag was not added or error.
When error is encountered, err object is returned:
{
error:true,
message: 'something went wrong'
}
Example 1:
First call back is called if tag has been successfully removed, second call back is called if error has occurred during a tag removal:
var Gravitec = Gravitec || [];
Gravitec.push([
"segmentation.removeTag", "value",
function() {console.log("Tag has been removed")},
function(err){console.log(err.message)
}])
Example 2:
Call back is called if tag has been successfully removed:
var Gravitec = Gravitec || [];
Gravitec.push([
"segmentation.removeTag ","value",
function() {console.log("Tag has been removed")
}])
Example 3:
Without call back functions:
var Gravitec = Gravitec || [];
Gravitec.push([
"segmentation.removeTag ", "value"
])
segmentation.removeAllTags
The method deletes all user’s tags that were previously set for a user with addTag, addTags or setTags.
Parameters:
- string value - set this to removeAllTag.
- Call back - set here call back to be fired, when tag was successfully added and response from our server has been returned.
- Call back - set here call back to be fired, when tag was not added or error.
When error is encountered, err object is returned:
{
error: true,
message: 'something went wrong'
}
Example 1:
First call back is called if all tags have been successfully removed, second call back is called if error has occurred during tags removal:
var Gravitec = Gravitec || [];
Gravitec.push([
"segmentation.removeAllTags",
function() {console.log("All tags has been removed")},
function(err){console.log(err.message)
}])
Example 2:
Call back is called if all tags have been successfully removed:
var Gravitec = Gravitec || [];
Gravitec.push([
"segmentation.removeAllTags",
function() {console.log("All tags has been removed")
}])
Example 3:
Without call back functions:
var Gravitec = Gravitec || [];
Gravitec.push([
"segmentation.removeAllTags"
])
getTags
Returns list of tag objects, associated with a subscriber.
Code:
var Gravitec = Gravitec|| [];
Gravitec.push(["getTags", callback]);
Example:
This code prints an array of the subscriber’s tags.
Gravitec.push(['getTags', function(tags) {
var newArray = [];
tags.map((tag)=>newArray.push(tag));
console.log(newArray);
}])
segmentation.setAlias
Set an alias(user identifier) for subscriber to target the one.
Parameters:
- string value - set this to setAlias.
- string value - value to set – alias name.
- Call back - set here call back to be fired, when tag was successfully added and response from our server has been returned.
- Call back - set here call back to be fired, when tag was not added or error.
When error is encountered, err object is returned:
{
error:true,
message: 'something went wrong'
}
Example 1:
First call back is called if alias has been successfully set, second call back is called if some error has occurred during alias setting:
var Gravitec = Gravitec || [];
Gravitec.push([
"segmentation.setAlias", "value",
function() {console.log("Alias has been set")},
function(err){console.log(err.message)
}])
Example 2:
Call back is called if alias has been successfully set:
var Gravitec = Gravitec || [];
Gravitec.push([
"segmentation.setAlias","value",
function() {console.log("Alias has been set")
}])
Example 3:
Without call back functions:
var Gravitec = Gravitec || [];
Gravitec.push([
"segmentation.setAlias", "value"
])
getSubscriptionData
To get visitor’s subscription detail such as subscription status, REGid, permission status and browser use this method. The method returns a subscription object in a resolved promise.
Parameters:
- string value - set this to getSubscriptionData.
- Call back - set here call back to be fired, when subscription data have been successfully fetched, data object contains subscription details.
- Call back - set here call back to be fired, when an error occurred.
When error is encountered, err object is returned:
{
error:true,
message: 'something went wrong'
}
Example 1:
var Gravitec = Gravitec || [];
Gravitec.push([
"getSubscriptionData", function(data) {
console.log(data.permission);
console.log(data.subscription);
if(data.subscription) {
console.log(data.subscription.regID);
}
}, function(err) {
console.log(err.message);
}
])
afterSubscription
Callback that is called after the device is successfully registered with Gravitec. Return token.
Example:
var Gravitec = Gravitec || [];
Gravitec.push(["afterSubscription", function (token) {
console.log(token);
//Your action
}]);
subscriptionResult
The method fires a callback after visitor interacted with subscription prompt. Visitor can press Allow, Block or just close the prompt in all these cases the method returns a relevant data.
Parameters:
- string value - set this to subscriptionResult.
- Call back - set here call back to be fired, when visitor press Allow, Block or closed the prompt, data object contains subscription details.
- Call back - set here call back to be fired, when an error occurred.
When error is encountered, err object is returned:
{
error:true,
message: 'something went wrong'
}
Example 1:
var Gravitec = Gravitec || [];
Gravitec.push([
"subscriptionResult", function(data) {
console.log(data.permission);
console.log(data.subscription);
if(data.subscription) {
console.log(data.subscription.regID);
}
}, function(err){
console.log(err.message);
}
])
getSubscription
Lets you retrieve the Google Registration ID. Your handler is called after the device is successfully registered with Gravitec.
Example:
var Gravitec = Gravitec || [];
Gravitec.push(["getSubscription", function (subscriptionId) {
if (subscriptionId) {
console.log(subscriptionId);
}
}]);
isSubscribed
Shows if user give permission to send notifications. Return true or false.
Example:
var Gravitec = Gravitec || [];
Gravitec.push(["isSubscribed", function (success) {
console.log(success);
//Your action
}]);