Callbacks¶
You can register named global functions that are called on certain events.
On User Has Access¶
If, according to our records, a user should have access to the current article, this callback function is called.
Example
In this example, the function myGlobalCallbackFunction()
function will be called
if the user has access to the paid content on the current page.
<!-- Define the global JavaScript callback function -->
<script type="text/javascript">
function myGlobalCallbackFunction() {
alert("Congratulations, you have access to this content!");
}
</script>
<!-- LaterPay Connector In-Page Configuration -->
<script type="application/json" id="laterpay-connector">
{
"callbacks": {
"onUserHasAccess": "myGlobalCallbackFunction"
}
}
</script>
<!-- Define the global JavaScript callback function -->
<script type="text/javascript">
function myGlobalCallbackFunction() {
alert("Congratulations, you have access to this content!");
}
</script>
<!-- LaterPay Connector In-Page Configuration -->
<meta property="laterpay:connector:callbacks:on_user_has_access" content="myGlobalCallbackFunction">
Refer to chapter In-Page Configuration Syntax Of Connector Script For Video Players for details about the syntax.
On Access Denied¶
If, according to our records, a user should be denied access to the current article_id
,
or full set of article_id
s this callback function is called.
This means that the user either did not yet purchase access to the content, or, the user purchased content in the past, but is not identified/authenticated and could restore access through logging in to LaterPay.
The function receives two parameters:
context
the string “Paid Content”done
Callback function
The done
callback allows for asynchronous continuation control of Connector Script: In most cases,
you should call it eventually to allow Connector Script to continue. However, you can choose
not to call it and thus prevent the rendering of the Purchase Overlay.
Return false
to suppress the default behavior of continuing the program flow.
Example
In this example, the function myGlobalCallbackFunction
function will be called
if the user is denied access to the current page.
<!-- Define the global JavaScript callback function -->
<script type="text/javascript">
function myGlobalCallbackFunction(context, done) {
setTimeout(done, 1000); // Symbolic example for "do some asynchronous task"
return false;
}
</script>
<!-- LaterPay Connector In-Page Configuration -->
<script type="application/json" id="laterpay-connector">
{
"callbacks": {
"onAccessDenied": "myGlobalCallbackFunction"
}
}
</script>
<!-- Define the global JavaScript callback function -->
<script type="text/javascript">
function myGlobalCallbackFunction(context, done) {
setTimeout(done, 1000); // Symbolic example for "do some asynchronous task"
return false;
}
</script>
<!-- LaterPay Connector In-Page Configuration -->
<meta property="laterpay:connector:callbacks:on_access_denied" content="myGlobalCallbackFunction">
Refer to chapter In-Page Configuration Syntax Of Connector Script For Video Players for details about the syntax.
On Tracking Event¶
Deprecated since version 2.12.0.
Warning
This callback was deprecated since Connector Script 2.12.0 and has been removed with Connector Script 3.
It has been renamed to On Analytics Event.
On Analytics Event¶
This callback function is called when analytics events are triggered. It allows for a Custom Analytics Integration.
The function receives two or three parameters:
eventAction
The Event Action, for example “Paid Content Show”done
Callback functiondata
(Optional) Additional Event Data, for example details of a purchase option. Only passed for some Event Actions, see section Event Actions for details.
By default, Connector Script sends a triggered Event Action to all detected analytics services (see Automatic Analytics Integration). Return false
to suppress the default behavior.
Note
When suppressing the default behavior (by returning false
), you should call the done
callback eventually. Otherwise, Connector Script will not be able to continue to current task, for example redirect to the purchase page after a user has clicked a purchase button.
Example
In this example, the function handleConnectorAnalyticsEvent
will be called
when analytics events are triggered. The function logs the Event Action and all additional Event Data.
By returning false
, the function suppresses Connector Script’s default behavior. The continuation of Connector Script is triggered after one second,
which is symbolic in this example for “do some asynchronous task”.
<!-- Define the global JavaScript callback function -->
<script type="text/javascript">
function handleConnectorAnalyticsEvent(eventAction, done, data) {
console.log("Received event action from Connector: "+eventAction);
console.log(data);
setTimeout(done, 1000); // Symbolic example for "do some asynchronous task"
return false;
}
</script>
<!-- LaterPay Connector In-Page Configuration -->
<script type="application/json" id="laterpay-connector">
{
"callbacks": {
"onAnalyticsEvent": "handleConnectorAnalyticsEvent"
}
}
</script>
<!-- Define the global JavaScript callback function -->
<script type="text/javascript">
function handleConnectorAnalyticsEvent(eventAction, done, data) {
console.log("Received event action from Connector: "+eventAction);
console.log(data);
setTimeout(done, 1000); // Symbolic example for "do some asynchronous task"
return false;
}
</script>
<!-- LaterPay Connector In-Page Configuration -->
<meta property="laterpay:connector:callbacks:on_analytics_event" content="handleConnectorAnalyticsEvent">
Refer to chapter In-Page Configuration Syntax Of Connector Script For Video Players for details about the syntax.
On Ready¶
Note
This callback is available in Connector Script Classic only.
The In-Page Configuration Callback “On Ready” can be registered to receive the lpcHandle
object which exposes functions for controlling the program flow of Connector Script.
The “On Ready” callback is invoked after Connector Script has been loaded and initialized but before
the API is requested. The lpcHandle
object is passed to the callback function as its first
and only parameter.
The following functions are exposed as properties of the lpcHandle
object:
reset
Resets Connector Script to its initial state and deobfuscates any obfuscated Paid Content
init
Initializes the app from local data (reads In-Page Configuration) but doesn’t request the API
fetch
Fetches data from the API based on the current configuration that was initialized by
init
preventDefault
When called immediately within the In-Page Config Callback “On Ready”, this disables automatic fetching of data from the API and thus stops the execution of Connector Script before it renders any UI into the DOM
Example
<!-- Define the global JavaScript callback function -->
<script type="text/javascript">
function lpcReadyCallback(lpcHandle) {
console.log('Received lpcHandle:', lpcHandle)
lpcHandle.preventDefault()
// Use functions `lpcHandle.fetch()`, `lpcHandle.reset()` and `lpcHandle.init()`
// to control the flow of Connector Script
}
</script>
<!-- LaterPay Connector In-Page Configuration -->
<script type="application/json" id="laterpay-connector">
{
"callbacks": {
"onReady": "lpcReadyCallback"
}
}
</script>
<!-- Define the global JavaScript callback function -->
<script type="text/javascript">
function lpcReadyCallback(lpcHandle) {
console.log('Received lpcHandle:', lpcHandle)
lpcHandle.preventDefault()
// Use functions `lpcHandle.fetch()`, `lpcHandle.reset()` and `lpcHandle.init()`
// to control the flow of Connector Script
}
</script>
<!-- LaterPay Connector In-Page Configuration -->
<meta property="laterpay:connector:callbacks:on_ready" content="lpcReadyCallback">
In the example above, the global function lpcReadyCallback
is registered as In-Page Configuration
Callback “On Ready”. It is invoked by Connector Script as soon as the app is loaded and initialized
but before the API is requested.
By calling lpcHandle.preventDefault()
the default behavior of Connector Script is disabled which
means that:
the API will not be requested
the Purchase Overlay will not be rendered
the Paid Content (elements matched by the Content Selector) will not be obfuscated
At this point you can use the functions exposed by the lpcHandle
object for custom control of
Connector Script.
A typical use case for this kind of fine-grained control is a Single Page Application:
Use
lpcHandle.preventDefault()
to prevent Connector Script from requesting the API and from rendering any UI automatically.Call
lpcHandle.init()
andlpcHandle.fetch()
as soon as your dynamic content is fully rendered.Call
lpcHandle.reset()
before changing the page content.Start from #2 again after new page content has been rendered.