Advanced Configuration

Syncronex provides a default implementation of the Paymeter libraries to reduce the amount of work required from a customer's CMS developers.  The default implementation only requires the two HTML tags on a metered page and then configuration of appropriate overlays and business rules. Sometimes the default implementation won't satisfy all of a customer's requirements for metering content.  In those cases, a CMS developer can leverage the configuration and events triggered by the PaymeterSdk directly.

Paymeter Events

The PaymeterSdk will trigger events throughout the authorization process.  You can write functions and attach them to these events to intercept the process and either examine data or set/override it to suit your requirements.

The PaymeterSdk exposes an events property that provides the entry point into the event handling mechanism.  You attach your functionality to these events using the registerHandler function:

registerHandler = function(event, handler){…}
event object Specific Paymeter event Id.
Handler function A callback function that will be executed when the event fires. Most events will pass an argument to the callback function.
The callback functions should return void (the event engine will ignore return values from callback functions).

You can un-register a callback function with the unRegisterHandler function:

unRegisterHandler = function(event, handler){…}

See above for argument descriptions.

Example:

// Example attaching a callback function to the onWarningFired event
(function handleWarning(sdk){
       // Record the fact that a paymeter warning was issued...
       sdk.events.registerHandler(sdk.events.onWarningFired, function(data){
              var viewCount = data.viewCount;   // get the current # of views
              var viewsLeft = data.remainingViewCount; // how many left?
              var sessionId = data.newSessionId;
              myCustomRecorder.RecordWarning(viewCount,viewsLeft,sessionId);
       });
})(window.syncPaymeterSdk);

This is the order in which these events will fire during the authorization check:

  1. Session Starting (onSessionStarting)
  2. Session Started (onSessionStarted)
  3. Authorization Starting (onAuthorizationStarting)
  4. Authorization Success (onAuthorizeSuccess), Authorization Failed (onAuthorizeFailed)
  5. Warning Issued (onWarningFired), Wall Issued (onWallFired), Server Error (onServerError)
  6. Session Saving (onSessionSaving)
  7. Session Saved (onSessionSaved)

Each of the events exposed by the PaymeterSdk is described in detail in this section.

Session Starting

The onSessionStart event is triggered by the PaymeterSdk at the beginning of the authorization process.  This event signifies that the library is gathering information about the current user and page content in preparation for the actual server API call.  This event will pass a SessionContext object into any registered callbacks (see below).

Example:

(function myContextLoad(sdk){
       sdk.events.registerHandler(sdk.events.onSessionStarting, function(ctx){
              // set the referring page to current page if there is no
              // actual referring page
              ctx.referringPage = window.document.referrer || window.location;
       });
})(window.syncPaymeterSdk);

Session Started

The onSessionStarted event is fired after the current session context is loaded and initialized.  This event will also pass a SessionContext object into any registered callbacks.

(function contextOverride(sdk){
sdk.events.registerHandler(sdk.events.onSessionStarted, function(ctx){
       // peek into the sessionId 
       var sessionid = ctx.user.sessionId;
       // override the contentId
       ctx.contentId = getCustomContentId();
});
 })(window.syncPaymeterSdk)

This event is the last chance to modify any of the session context before the authorization call.  The Session context is fully loaded at this point so you can examine current values and override if necessary.

Session Saving

The onSessionSaving event is fired after the authorization call has completed and signals the point at which the PaymeterSdk is beginning the process of saving session data to the local cookie.  You can intercept this event to change any of the session values before they are saved.

The Paymeter Sdk will pass the current Session Context into any registered callback functions for this event.

Session Saved

The onSessionSaved event is fired once the session context has been saved. There are not arguments passed to the callback functions registered to handle this event. 

Session Context Object

The Session Starting, Session Started, and Session Saving events will pass a Session Context object into any registered callback functions. The Session Context Object maintains the data that is required for authorizing a user.  Some of this data is persisted from one user session to the next (like SessionId, userId, etc.) and other data is specific to the current page (like currentPage, referringPage, and contentId).

Session data that must persist from page to the next is stored in a local cookie named: syncmeter-user-ctx.

// sessionContext
{
       'user':{
                     'sessionId':string,
                     'userId':int,
                     'authToken':string,
                     'externalId':string
              },
       'currentPage':string,
       'referringPage':string,
       'contentId':string,
       'channel':string,
       'pageHistory':object,
       'clientInfo':string
}
user object Holds user-specific context data
   sessionId String Holds a unique Paymeter SessionId. This is an anonymous identifier used to record content view counts and to identify a user before she has authenticated. The initial value is provided by the server API response.
   userId Int Holds a unique user identifier once the user has authenticated. This is the unique, internal id for that user within the Digital Paymeter system.
   authToken String Reserved for future use. 
   externalId String Holds an external unique Identifier or token from a third party IDP system. 
currentPage String Holds the url of the current Page being viewd.
referringPage String Holds the url of the page from which the current page was linked.
contentId String Holds the content category for the current page
channel String Reserved for future use.
pageHistory Object See Page History section below
clientInfo String Holds information specific to the client browser. By default, this is the browser's userAgent string.

Session context is loaded at the beginning of the authorization process and then saved upon completion of the authorization process.

Authorization Starting

The onAuthorizeStarting event is triggered immediately before the server authorization API is called.  There is no data passed into any registered callbacks for this event so there is no opportunity at this point to change the impending server call.  You can bind functions to this event to track workflow (analytics data, logging, etc.).

Authorization Success

The onAuthorizeSuccess event is triggered if the current user has been authorized to view the current content.  The Paymeter SDK will pass an Authorization Context Object into any registered callback functions (see below).

Example:

(function authorizeSuccessOverride(sdk){
       sdk.events.registerHandler(sdk.events.onAuthorizeSuccess,function(ctx){
              console.log('paymeter says user is authorized. ' + ctx.statusMsg);
       });
})(window.syncPaymeterSdk);

Authorization Failure

The onAuthorizeFailed event is triggered whenever the server responds that the given user is not allowed to view the given content.  This usually occurs when a user has exhausted her free views of the content but can also happen if a known, authenticated user does not have appropriate plans to grant access.  The SDK will pass a Authorization Context Object into any registered callback functions (see below).

Example:

(function authorizationFailOverride(sdk){
       sdk.events.registerHandler(sdk.events.onAuthorizeFailed, function(ctx){
              console.log('User not authorized. ' + ctx.statusMsg);
       });
})(window.syncPaymeterSdk);

Note: the onAuthorizationFailed event will usually be preceded by the onWallFired event.

Paywall Triggered

An authorization failure usually results in a meter Paywall (the default implementation of the PaymeterSdk automatically loads and renders a pre-defined paywall HTML overlay).  The onWallFired event is triggered if the server responds indicating that the user is not authorized to view content.  This event will fire immediately before the onAuthorizeFailed event.  Like the other authorization events, this one will pass an Authorization Context Object into any registered callback functions.

Example:

(function paywallOverride(sdk){
       sdk.events.registerHandler(sdk.events.onWallFired, function(ctx){
              console.log('Time to show a paywall!. ' + ctx.statusMsg);
              myEventTracker.trackWallHit(ctx.newSessionId);
       });
})(window.syncPaymeterSdk);

Warning Triggered

The onWarningFired event is triggered when a user has hit a specific number of views that have been configured as a "warning threshold".  Administrators can define specific view counts for which a warning (aka 'alert') might be displayed to the user giving them some information about remaining free views, special offers, etc.

A user is still considered to be 'authorized' when the onWarningEvent is fired; however, the onAuthorizeSuccess event is not actually triggered if a user has reach a warning threshold.   An Authorization Context Object is passed to any registered callback functions.

Example:

(function warningOverride(sdk){
       sdk.events.registerHandler(sdk.events.onWarningFired, function(ctx){
              var cnt = ctx.viewCount;
              var total = ctx.viewCount + ctx.remainingViewCount;
              var msg = 'User has read ' + cnt.toString() +
                         ' out of ' + total.toString();
              console.log('Warning to user!.' + msg  );
});
})(window.syncPaymeterSdk);

Server Error (API Error)

The onServerError event is triggered if there is an unexpected error reported from the server. This is rare but when it does occur, the client must decide the best way to handle it.  The default Paymeter implementation will silently ignore server errors in order to prevent readers from being blocked from reading content when environmental circumstances prevent the Paymeter API from operating as expected.  The same Authorization Context Object is included in any registered callbacks and can be queried to get specific error details.

Example:

//     Report API timeout errors
(function reportTimeoutErrors(sdk){
       sdk.events.registerHandler(sdk.events.onServerError,function(ctx){
              myCustomBugTracker.RecordBug('Paymeter server error: ' +
                     ctx.statusCode + ':' + ctx.statusMsg);
});

})(window.syncPaymeterSdk);

Authorization Context Object

The authorization context object is sent to callback functions that are bound to the onAuthorizeSuccess, onWarningFired, onWallFired, and onAuthorizeFailed events.  The object provides contextual information about the authorization call to the server.

//     Authorization Context Object
//
{
'newSessionId':string,
'statusCode':string,
'statusMsg':string,
'remainingViewCount':int,
'viewCount':int,
'authorized':bool,
'registerUrl':string,
'error':bool
}
newSessionId String Provides a new Anonymous Session Identifier provided there wasn't one already stored in the Session Context.
This value is subsequently persisted to the Session Context and sent back the server on future authorization calls.
(typically a GUID but there are no guarantees as to the format of the identifier). It will, however, be a guaranteed unique identifier.
statusCode String The status returned by the server indicating the outcome of the authorization call. This is a string property but holds numeric values:
0              Success status. User is authorized to view the content either because there are allowable free views left or because the user is an authenticated user with appropriate subscription level to access content.
100         Warning status.  User has access to content but has crossed a defined warning threshold (same as 103).
101         Success status. User is authorized because she linked to the specific content from an exempted referring site.
102         Success status.  User is authorized because she is viewing content from an exempted IP address.
104         Success status. User is authorized because she linked to the specific content from a referring site that has been configured to allow extra free views.
105         Success status. User is authorized because she linked to the specific content from Google (Google First Click Free)
106         Success status. User is authorized because he's access content designated as 'free' content.
200         Failure status.  User has exceeded her allowed free views of the given content.
201         Failure status. User has an insufficient subscription level to access the given content.
202         Failure status. Server error occurred.
203         Failure status. User is authenticated and known but has no active plans.
In general, 0-199 codes would indicate successful authorization (user can see the content) while 200-299 would indicate authorization failures.
statusMsg String A user-friendly message that accompanies the status Code and provides further details on the authorization status.
remainingViewCount Int A number indicating the amount of free 'views' to the current content Id that are left for this user.  If content is metered, users get a configurable amount of free views per 30 days (the 30 day count starts from the first access to a given category)
viewCount Int The current number of views the user has exhausted against the current content Id.  viewCount + remainingViewCount  = Total Meter Threshold
authorized Bool A Boolean value used mostly for backward compatibility.  This property will be true if statusCode < 200 and false if statusCode ≥ 200
registerUrl String Reserved for future
error Bool Flag indicating whether or not an error was encountered during the server call.