In the series Advanced Scripting Sample Apps we explain some use cases on how to use 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 to use the twxxml
and twxjson
functions. These allow you to parse XML and JSON data structures. There are often used in combination with the twxhttp module as most web services return either JSON or XML.
Some use cases:
- Based on the Entitlement Token, you can issue for example a request to the Entitlement Server to get more information about the user and change the content based on that information. Your Entitlement Server can output XML or JSON so that the advanced script can easily parse this data.
- In the
setupFilter
, you might want to issue an HTTP request to your ad system to find out which of your ads are still valid so that you can use that information to hide the invalid ads in your app. If your ad server returns XML or JSON, you can easily parse the response with the providedtwxxml
andtwxjson
modules.
Instructions
Preparations
- Create a new app
- Set up your Advanced Scripting (see below)
- Run the app in the Twixl app and then go to the Gear menu > Advanced Filter Log to see the log messages.
- For the browser client, open the root collection, append
?debug=1
to the url and you'll see the debug icon in the toolbar.
Advanced Scripting Settings
For the Root Collection:
// Executed once for every collection before the filtering of the items function setupFilter(collection, environment) { // Get some json var json = '{"name": "Twixl media", "items": ["item 1", "item 2"]}'; // Parse the json and log it var parsedJson = twxjson.Parse(json); twxlog.InfoDump(parsedJson); // Encode json var data = {"name": "Twixl", "items": ["item 1", "item 2"]}; twxlog.Info(twxjson.Encode(data)); // Get some xml var xml = '<?xml version="1.0" encoding="UTF-8" standalone="no"?><data><name>Twixl Publisher</name><items count="2"><item>Item 1</item><item>Item 2</item></items></data>'; // Parse the xml and log it var parsedXml = twxxml.ParseString(xml); twxlog.Info(parsedXml.String()); twxlog.InfoDump(parsedXml.Element("name").Text()); twxlog.InfoDump(parsedXml.Element("items").Attr("count", 0)); twxlog.InfoDump(parsedXml.Element("items").Elements("item")); } // Executed for once for every collection function shouldShowCollection(collection, environment) { return true; } // Executed for every single content item function shouldShowItem(contentItem, collection, environment) { return true; } // Executed once for every collection after the filtering of the items function teardownFilter(collection, environment) { }