Cloudflare Docs
Zaraz
Visit Zaraz 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 clicking 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 Zaraz event and system properties .

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

Rule typeVariable nameMatch operationMatch string
Match rule{{ client.__zarazTrack }}Equalspurchase

When matching based on a System property, you will often want to add a second rule that matches {{ client.__zarazTrack }} to 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 rule{{ system.page.url.pathname }}Starts with/blog
Rule typeVariable nameMatch operationMatch string
Match rule{{ client.__zarazTrack }}EqualsPageview

Trigger example: All logged in users

Rule typeVariable nameMatch operationMatch string
Match rule{{ system.cookies.isLoggedIn }}Equalstrue
Rule typeVariable nameMatch operationMatch string
Match rule{{ client.__zarazTrack }}EqualsPageview

Refer to the Zaraz event and system properties 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 rule{{ system.page.url.pathname }}Equals/my-page-path

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
Form submission

Tracks form submissions using CSS selectors. Click 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 rule{{ system.page.url.pathname }}Equals/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 rule{{ system.page.url.pathname }}Equals/my-page-path

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