← Back to API
Define Targeted Chat rules
olark('api.rules.defineRule', { options });
Arguments
- options.action
function that performs the action, e.g. notifying the operator (see examples below) - options.condition
function that evaluates a condition and calls pass when true (see examples below) - options.description
human-readable description of what this rule does, e.g. 'notifies the operator about important visitors' - options.id
unique identifier for this rule, e.g. _important_visitor_rule_1_ - options.perpage
make this true if the action is only supposed to trigger once per page. OR:: - options.pervisit
make this true if the action is only supposed to trigger once per visit OR: - options.pervisitor
make this true if the action is only supposed to trigger once per visitor
Notes
Note: You can create Targeted Chat rules without any coding on the Targeted Chat settings page.
Create Targeted Chat rules that help you automate decisions about how and when to interact with visitors and operators. You can create rules to perform actions such as:
- Initiating chat with a visitor who has been browsing for more than 30 seconds
- Notifying an operator when a visitor lands on the page from a Google AdWords campaign
- Highlighting important visitors in your buddy list
Be sure to check out the getDetails API call to learn how to access detailed customer information for making creative rules.
Start a conversation with a visitor after 5 pageviews
Let’s say you wanted to auto-initiate with any visitor who has visited 5 pages without talking to an operator, since maybe he is confused:
<script>
olark('api.rules.defineRule', {
// Specify a unique ID for this rule.
// This helps the API to keep your rules separate from each other.
id: '1',
// The description summarizes what this rule does
description: "offer help to a visitor after he has browsed 5 pages and hasn't chatted yet",
// The condition will be checked whenever there is a relevant change in the chat.
// Call the pass() function whenever the criteria is met
condition: function(pass) {
// Use the Visitor API to get information the page count
olark('api.visitor.getDetails', function(details){
if (details.pageCountForThisVisit >= 5 && !details.isConversing) {
// The visitor has seen more than 5 pages, and the visitor hasn't started chatting yet
pass();
}
});
},
// The action will be executed whenever the condition passes.
// Limit the number of times this action will trigger using the perPage, perVisit, and perVisitor options.
action: function() {
olark('api.chat.sendMessageToVisitor', {
body: "hi, have any questions about our products?"
});
},
// Restrict this action to execute only once per visit
perVisit: true
});
</script>
Start a conversation on specific pages
Maybe you would like to hide the chatbox on certain pages, but only if the visitor is not already chatting:
<script>
olark('api.rules.defineRule', {
// Specify a unique ID for this rule.
// This helps the API to keep your rules separate from each other.
id: '2',
// The description summarizes what this rule does
description: "hide the chatbox when the visitor is not chatting and is viewing an unimportant page",
// The condition will be checked whenever there is a relevant change in the chat.
// Call the pass() function whenever the criteria is met
condition: function(pass) {
// Check if the visitor is already in conversation
// ...and whether they're on a specific page
olark('api.visitor.getDetails', function(details){
// Determine whether this page is important
// The URL can be whatever you like
var isImportantPage = (details.currentPage.url.indexOf('/important-page') >= 0);
if (!details.isConversing && !isImportantPage) {
// Visitor is not chatting yet
// and they are viewing an unimportant page
pass();
}
});
},
// The action will be executed whenever the condition passes.
// Limit the number of times this action will trigger using the perPage, perVisit, and perVisitor options.
action: function() {
// Hide the chatbox
olark('api.box.hide');
},
// Restrict this action to execute only once per page
perPage: true
});
</script>