ONEcount Javascript SDK Reference

Last modified by rayaan@one-count_com on 2022/11/02 19:35

The ONEcount SDK is available from the main javascript widget that is loaded on the client site all.min. Using the  SDK, developers can interact with the platform as well as hooking into events. All of the code necessary for running the Javascript SDK is loaded through all.min.js.

Methods

  • GCN.onecount.login(brand): If the user is not already logged in, this will take the user to the login page.
    • brand: optional parameter that specifies with brand to user on the login page.
  • GCN.onecount.logout(): Log a user out of ONEcount if the user is logged in.
  • GCN.onecount.isLoggedIn(): Check whether a user is logged in or not. Return a boolean true/false
  • GCN.onecount.setTargeting(): Load the targeting rules if any
  • GCN.onecount.identify(data, create_flag): Identifies a user with data that is matched against a demographic field. This is useful when you have a data point like e-mail or some other unique identifier that has been uploaded into ONEcount, and you want to identify a web site visitor using that field.
    • data: This must be JSON ({"demo_id":"value"}). There can be multiple key-value pairs(properties) under JSON object, but all fields must be true in order for the user to be identified
      • demo_id: this is the numeric question ID of the demographic field you want to query
      • value: the value for the field you are querying.
    • create_flag: This must be Boolean value either true or false. Tells ONEcount whether to create a new user with this identifier if no match is found. 
      For example, we tell ONEcount that the current user has an e-mail address (question #1) of test9@gmail.com and a first name (question #4) of "Ray", and if no current user matches, to create a new user with this information, you would use:  
      GCN.onecount.identify({"1":"test9@gmail.com","4":"ray"},true); {"success":true}

  • GCN.onecount.custom_data(identifier, value, callback): Collects custom data provided by clients.
    • identifier: the unique identifier for that type of data
    • value: the value we're collecting. This must be a JSON string ('{"foo" : "bar"}')
    • callback: the callback function to call. A object will be sent as a parameter with the value {"success":true} for successful and {"success":false} for unsuccessful.
  • GCN.onecount.track(url, title, referrer, resourceId, callback): By default, ONEcount widgets track impressions on each page load. For sites where one article is displayed per page, this works fine. But in cases where articles are displayed on the same page, i.e. infinite scroll, only the first one is tracked.

This function is designed to provide an impression track for each article on a page. The function expects three parameters:

      • url: The url of the article that is supposed to be tracked. This is required.
      • title: This should be the same as the title tag if the article was displayed alone on the page.
      • referrer: The referrer of article. (For infinite scroll this can be the previous article in the scroll.)
      • resourceId: this is optional. If set to a valid resourceId, that resource that will be used in order to perform gating or increase view_count. You can pass 0 in order to use the default resource associated with the url if any

      • callback: function to be called after a request has been sent to ONEcount. This can return the number of articles read by the user for products with limit in timeframe

While the impressions are being tracked, ONEcount will also check whether the user's subscription is still valid or not (e.g. limit of X articles per month has been reached). If the subscription is not valid, then the user will be taken to the subscription page.

Let's assume I have a page where the first article is displayed when the page loads and three mores articles are displayed as the user scrolls down. The first article will be tracked by ONEcount, but for the three other articles, a call to GCN.onecount.track needs to be made to tell ONEcount that the articles have been displayed. The url should be the direct url of the article. The referrer should be the previous article's url.

  • GCN.onecount.gate(resourceId, callback):

Gating is currently done based on a REGEX match of a url segment on page load. This function is designed to allow customers to gate content where gating can not be sufficiently managed using URL segments, or where content must be gated without a page load.

This function expects two parameter :



      1. resourceId : this is the resourceId that will be used for gating and it's required.
      2. callback: this is a function that can be passed. The response from the gating request will be made available in the callback. The callback should expect one parameter which is a JSON object. The JSON object has a property called hasAccess which is a boolean (true/false). true means the user has access and false means he does not.

Resource Ids are available in ONEcount on the resource listing page.

To create a resource that uses this function, choose “Function” from the resource type pull-down, then enter a text return value in the box below. This return value is ignored for now, but may be used to trigger gating in the future.

When the function is called and the user is not subscribed to the product that the resource belongs to, he will be taken to the registration page in order to do so. If he is already subscribed to the product then nothing will happen.


Events

The SDK, provides promises that you can user in order to take some action at certain point. The 3 type of events are init, login and logout. You can subscribe to these events using the function below.

GCN.on(type): This function returns a promise, which will return data once it’s fulfilled depending on the type. type can be init, login or logout.

  1. login: this is fulfilled once a user log in. Once the promise is fulfilled an object with the ocid and username are made available.

  2. logout: this is fulfilled once the use log out. Once the promise is fulfilled an empty object is returned.

  3. init: this is fulfilled on every page load after the widget has completed the initialization. This returns an object with the following properties:

    • id: int - user’s id

    • username: string - user’s username

    • email: string - user’s email

    • subscription: array - this is an array containing the ids of the products the user is subscribed to

    • loggedin: boolean – flag to tell if the user is logged in or not

    • bypass: mixed - this will return false if the user is not bypassed. If the user is bypassed then this will be an object with the properties type and value. (type ip will also have a name property). Type can be url, user_agent, referrer or ip. Value will contains the value based on which the bypassed occurred

    • view_count: int – view_count is only displayed for products/packages that are protected by a frequency/time frame. Some products have a limit like 10 articles per day or 3 articles per month, it will hold the value of articles that have been read. If the product is not protected by a frequency/time-frame, this value will be -1.

    • cookie: object – return the user’s ONEcount cookie


Common Use Case

You will probably need to change your site o dsplay a welcome mssage after a user has been logged in. Below is an example on how to achieve this assuming you have a dom called loginurl,

Welcome message for logged in user
GCN.on("init").then(function(data) {

 //If user is logged in, let's change the link
 if (data.loggedin) {
   var html = '<span style="font-size=12px;">Welcome <b>' + data.username + ' </b></span> <span><a href="javascript:GCN.onecount.logout()">Logout</a> </span>';
   
    $( document ).ready(function() {
     //Get the dom
     var dom = $("#loginurl");
     
      dom.replaceWith(html);
    });
   
  }
 
});