In the series Advanced Scripting Sample Apps we explain some use cases for the Advanced Scripting. For more general info, it's very important that you read this KB-article first.
Use Case
In this example, we show how you can add a Privacy Policy to your app which users have to accept before they are granted access to your app.
Instructions
Preparations
- Create a new app
- Add a Cell Style for:
- The Privacy Policy
- The button to accept the Privacy Policy
- The default content
- Add the following contents items to the Root Collection with the following properties:
- Privacy policy
- Type: Embedded Web Viewer
- Name:
privacy.policy
- Title:
Privacy Policy
- Content: zipped .html file with the privacy policy
- Button to accept the Privacy Policy
- Type: Web link
- Name:
privacy.accept
- Title:
Accept & Continue
- Link to URL:
tp-set-custom-vars://?has_accepted=yes&version=20190125
- The actual content (just ensure that the name doesn't start with privacy).
- Privacy policy
- Set up your Advanced Scripting (see below)
Advanced Scripting Settings
For the Root Collection
// Keep a variable to check if the user accepted the privacy policy
var hasAccepted = false;
// Executed once for every collection before the filtering of the items
function setupFilter(collection, environment) {
hasAccepted = environment.CustomVar("has_accepted") == "yes";
}
// Executed once for every collection
// Root and hamburger are always shown
function shouldShowCollection(collection) {
return hasAccepted;
}
// Executed for every single content item
function shouldShowItem(contentItem, collection, environment) {
if (contentItem.Name.startsWith("privacy.")) {
return !hasAccepted;
}
return hasAccepted;
}
// Executed once for every collection after the filtering of the items
function teardownFilter(collection, environment) {
}
Extra exercise 1 - Clearing the setting
Preparations
- Add a the following content item to the Hamburger Menu Collection
- Button to accept the Privacy Policy
- Type: Web link
- Name:
<default>
- Title:
Clear Privacy Policy
- Link to URL:
tp-set-custom-vars://
- Button to accept the Privacy Policy
- Set up your Advanced Scripting (see below)
Advanced Scripting Settings
For the Hamburger Menu Collection:
// Executed once for every collection before the filtering of the items
function setupFilter(collection, environment) {
}
// Executed for every single content item
function shouldShowItem(contentItem, collection, environment) {
var hasAccepted = environment.CustomVar("has_accepted") === "yes";
return hasAccepted;
}
// Executed once for every collection after the filtering of the items
function teardownFilter(collection, environment) {
}
Extra exercise 2
Try to improve the code and add an extra check to find out which version of the privacy policy was accepted. If it's different than what has been accepted before, you should force the user to agree to it again…