We’ve migrated our documentation to a new site, which means some URLs have changed.
Subscriptions

How to pass data to Chartbeat

Use the Piano JavaScript library to obtain the user's login status as well as the user's access level. We run two calls in the below code to get this information.

Then, you can pass that information to Chartbeat by using the three values they suggest by default:

  • Anonymous user

  • Logged-in user

  • Logged-in user with paid subscription

In the example code, we are checking if a user has access to a resource with {rid: 'abc123'}. Naturally, you will need to change this to meet your needs as they pertain to your application.

Additionally, you could use the /access/list as well and loop through the returned array for the user and make the check if the access.granted attribute is true.

// initialize Chartbeat and TP objects
tp = window.tp || []
var _cbq = window._cbq = (window._cbq || []);

// To get user authentication status you would use the isUserValid() method available on the user object
var authStatus = tp.user.isUserValid()


// To check whether a user has access to resource "test", you can use the API functionality built into our javascript library
tp.api.callApi('/access/check', { rid: "abc123" }, function (response) {
  var hasAccess = response.access.granted

  // From here we can send either a user or subscriber level to Chartbeat
  if (authStatus && hasAccess) {
    _cbq.push(['_acct', 'paid']);
  } else if (authStatus) {
    _cbq.push(['_acct', 'lgdin']);
  } else {
    _cbq.push(['_acct', 'anon']);
  }
})

Last updated: