Introduction

Appgain.io Web SDK is the Way you can use Appgain SDK Features for your Website.

Installation

  • Download appgainsdk.js , firebase-messaging-sw.js and firebase.js .

  • Add them to the root of the Website (note that firebase will try to access firebase-messaging-sw.js at the root of your website ex:'website.com/firebase-messaging-sw.js').

  • Enable https on your Website

  • Import the Appgain SDK in all of your website pages

Importing

Once you add those two scripts to your web page you get access to the Appgain object that provides you with the SDK Methods


<head>
    <script src="https://www.gstatic.com/firebasejs/3.7.3/firebase.js"></script>
    <script type="text/javascript" src="/appgainsdk.js"></script>
</head>

Initializing

Initializing Appgain SDK using Appgain.init requires an appgainConfig object that includes

  • projectId: from Appgain Dashboard -> Project Settings -> Basic Settings ->API and SDK Keys

  • apiKey: from Appgain Dashboard -> Project Settings -> Basic Settings ->API and SDK Keys

  • userId [optional]: If the userId is not passed into the appgainConfig object, the sdk will search for the userId in

the localStorage or in the url query, if no userId has been found the

SDK generates a custom userId.


let  appgainConfig  = {
    projectId: "<appgain-projectId>",
    apiKey: "<appgain-apiKey",
    userId: "userId", //optional
};

Appgain.init(appgainConfig)
.then((res) => {
    console.log(res);
})
.catch((err) => {
    console.log(err);
});

SDK Methods

Once you call Appgain.init(config) you will able to call the rest of the SDK Methods.

Appgain.updateUser: This method takes a userInfo:JSON as a param and updates the current active User (generated by the Appgain.init) with the new values.

Appgain.logPurchase: This method takes (productName, amount(total paid money), currency, campaignId) as input params to log user's purchase action.

Appgain.logEvent: This method takes (eventName, amount, action, extras:json) as input params to log a custom user event, also note that extras requires a {key: value}.

Appgain.webPushSubscribe: This method asks the User to Enable browser Notifications for this website, note that it gets called automatically by calling the Appgain.init method.

Appgain.webPushUnSubscribe: This method unsubscribes the current User from our Notification System.

Updating User info (Appgain.updateUser)

You can update existing user info or add new data by calling updateUser() method.


let  userData  = {
    name: "Username",
    email: "userEmail@example.com",
};

Appgain.updateUser(userData)
.then((res) => {
    console.log(res);
})
.catch((err) => {
    console.log(err);
});

Transaction logging (Appgain.logPurchase)

Appgain.logPurchase(productName, amount(total paid money), currency, campaignId)


Appgain.logPurchase("productName", 100, "currency", "campaignId")
.then(res =>{
    console.log(res)
})
.catch (err=>{
    console.log(err)
})

Events Logging (Appgain.logEvent)


let  type  =  "type";
let  action  =  "action";
let  extras  = { key: "value" };

Appgain.logEvent(type, action, extras)
.then((res) => {
    console.log(res);
})
.catch((err) => {
    console.log(err);
});

Marketing Automation (Appgain.fireAutomator)


fireAutomator("triggerPoint", { key: "value" })
.then((res) => {
    console.log(res);
})
.catch((err) => {
    console.log(err);
});