Cloudflare Docs
Zaraz
Edit this page on GitHub
Set theme to dark (⇧+D)

Triggers and rules

Triggers define the conditions under which a tool will start an action. In most cases, your objective will be to create triggers that match specific website events that are relevant to your business. A trigger can be based on an event that happened on your website, like after selecting a button or loading a specific page.

These website events can be passed to Cloudflare Zaraz in a number of ways. You can use the Track method of the Web API or the dataLayer call. Alternatively, if you do not want to write code to track events on your website, you can configure triggers to listen to browser-side website events, with different types of rules like click listeners or form submissions.

​​ Rule types

The exact composition of the trigger will change depending on the type of rule you choose.

​​ Match rule

Zaraz matches the variable you input in Variable name with the text under Match string. For a complete list of supported variables, refer to Properties reference.

Trigger example: Match zaraz.track("purchase")

Rule typeVariable nameMatch operationMatch string
Match ruleTrack NameEqualspurchase

If you create a trigger with match rules using variables from Page Properties, Cookies, Device Properties, or Miscellaneous categories, you will often want to add a second rule that matches Pageview. Otherwise, your trigger will be valid for every other event happening on this page too. Refer to Create a trigger to learn how to add more than one condition to a trigger.

Trigger example: All pages under /blog

Rule typeVariable nameMatch operationMatch string
Match ruleURL pathnameStarts with/blog
Rule typeVariable nameMatch operationMatch string
Match ruleTrack NameEqualsPageview

Trigger example: All logged in users

Rule typeVariable nameMatch operationMatch string
Match ruleCookie: name: isLoggedInEqualstrue
Rule typeVariable nameMatch operationMatch string
Match ruleTrack NameEqualsPageview

Refer to Properties reference for more information on the variables you can use when using Match rule.

​​ Click listener

Tracks clicks in a web page. You can set up click listeners using CSS selectors or XPath expressions. Wait for actions (in milliseconds) tells Zaraz to prevent the page from changing for the amount of time specified. This allows all requests triggered by the click listener to reach their destination.

Trigger example for CSS selector:

Rule typeTypeSelectorWait for actions
Click listenerCSS#my-button500

To improve the performance of the web page, you can limit a click listener to a specific URL, by combining it with a Match rule. For example, to track button clicks on a specific page you can set up the following rules in a trigger:

Rule typeTypeSelectorWait for actions
Click listenerCSS#myButton500
Rule typeVariable nameMatch operationMatch string
Match ruleURL pathnameEquals/my-page-path

If you need to track a link of an element using CSS selectors - for example, on a clickable button - you have to create a listener for the href attribute of the <a> tag:

Rule typeTypeSelectorWait for actions
Click listenerCSSa[href$='/#my-css-selector']500

Refer to Create a trigger to learn how to add more than one rule to a trigger.


Trigger example for XPath:

Rule typeTypeSelectorWait for actions
Click listenerXPath/html/body//*[contains(text(), 'Add To Cart')]500

​​ Element Visibility

Triggers an action when a CSS selector becomes visible in the screen.

Rule typeCSS Selector
Element Visibility#my-id

​​ Scroll depth

Triggers an action when the users scrolls a predetermined amount of pixels. This can be a fixed amount of pixels or a percentage of the screen.

Example with pixels

Rule typeCSS Selector
Scroll Depth100px

Example with a percentage of the screen

Rule typeCSS Selector
Scroll Depth45%

​​ Form submission

Tracks form submissions using CSS selectors. Select the Validate toggle button to only fire the trigger when the form has no validation errors.

Trigger example:

Rule typeCSS SelectorValidate
Form submission#my-formToggle on or off

To improve the performance of the web page, you can limit a Form submission trigger to a specific URL, by combining it with a Match rule. For example, to track a form on a specific page you can set up the following rules in a trigger:

Rule typeCSS SelectorValidate
Form submission#my-formToggle on or off
Rule typeVariable nameMatch operationMatch string
Match ruleURL pathnameEquals/my-page-path

Refer to Create a trigger to learn how to add more than one condition to a trigger.

​​ Timer

Set up a timer that will fire the trigger after each Interval. Set your interval time in milliseconds. In Limit specify the number of times the interval will run, causing the trigger to fire. If you do not specify a limit, the timer will repeat for as long as the page is on display.

Trigger example:

Rule typeIntervalLimit
Timer50001

The above Timer will fire once, after five seconds. To improve the performance of a web page, you can limit a Timer trigger to a specific URL, by combining it with a Match rule. For example, to set up a timer on a specific page you can set up the following rules in a trigger:

Rule typeIntervalLimit
Timer50001
Rule typeVariable nameMatch operationMatch string
Match ruleURL pathnameEquals/my-page-path

Refer to Create a trigger to learn how to add more than one condition to a trigger.