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>
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]);

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([
  "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([
  "addTag", "Tag1", 
  function() {console.log("Tag has been added")} 
])

Example 3:

Without call back functions:

var Gravitec = Gravitec || []; 
Gravitec.push([
  "addTag", "Tag1"
])

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([
  "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([
  "addTags",[“new”,”new1”,”new2”], 
  function() {console.log("Tags has been added")} 
])

Example 3:

Without call back functions:

var Gravitec = Gravitec || []; 
Gravitec.push([
  "addTags", [“new”,”new1”,”new2”]
])

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([
  "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([
  "setTags",["value1", "value2"], 
  function() {console.log("Tags has been set")}  
])

Example 3:

Without call back functions:

var Gravitec = Gravitec || []; 
Gravitec.push([
 "setTags", ["value1", "value2"]
])

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([
  "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([
  "removeTag ","value", 
  function() {console.log("Tag has been removed")}   
])

Example 3:

Without call back functions:

var Gravitec = Gravitec || []; 
Gravitec.push([
  "removeTag ", "value"
])

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([
  "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([
  "removeAllTags",  
  function() {console.log("All tags has been removed")}  
])

Example 3:

Without call back functions:

var Gravitec = Gravitec || []; 
Gravitec.push([
  "removeAllTags"
])

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([
  "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([
  "setAlias","value", 
  function() {console.log("Alias has been set")}   
])

Example 3:

Without call back functions:

var Gravitec = Gravitec || []; 
Gravitec.push([
  "setAlias", "value" 
])

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);
 }
}]);

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
}]);

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
}]);