Rule Exercises
Objective
Explore fundamentals of KRL and building rules.
Preparation
- Read Chapter 6 in The Live Web.
- Complete the (Classic) Quickstart for SquareTag
- Read and understand (Classic) Tips for Developers. I strongly recommend using either the command line or Web-based parser to check your ruleset for syntax errors before you try to run it.
Exercises
The following exercises will use skills you learned in the Quickstart, but you won't be deploying your app on SquareTag.
- Create a ruleset that fires a notification on
ktest.heroku.com
. You will need to register it using KDK, but you won't have to install it in SquareTag for it to run onktest
. - Modify the rule you created in (1) to place two notification boxes on the page from the same rule.
- Add a second rule that places a third notification box on the page. This notification box should say "Hello " followed by the value of query string. If the query string is empty, say "Hello Monkey". You may find the
url
function in thepage
library helpful. - Write a function that given a string in standard URL encoding (e.g.
<key0>=<value0>&<key1>=<value1>...
) returns the value of the keyname
. (Hint, regular expressions and the string operators will be useful here.) Use function with the query string to modify the rule from (3) so that the notification says "Hello " followed by the value of the keyname
. Do not assume that the query string will contain only one key-value pair. So, if the url werehttp://ktest.heroku.com/b16x12232?name=Phil
, the notification would say "Hello Phil". Be sure you still default to "Hello Monkey" if there is no query string or there is no key calledname
. - Write a rule that counts the number of times it has fired and stops showing its notification after five times for any given user. Display the count in the notification.
- Write a rule that clears the count from (5) if a query string parameter named
clear
is given in exampley.com URL. - How would you modify the rule in (5) so that it stops showing the notification after it has been shown five times altogether (not just to a specific user)?
Copyright Picolabs | Licensed under Creative Commons.