**Cloudflare Zaraz: Enhancing the Loading of Third-Party JavaScript Tools**
**Why Use Zaraz for Loading Third-Party JavaScript Tools?**
Cloudflare users have been reaping the benefits of using Zaraz for loading their third-party JavaScript tools. By leveraging Zaraz, tools like analytics, conversion pixels, and widgets load faster and are safer.
**Enhance Data Transmission with Zaraz**
When configuring a tool in Zaraz, users can specify the payload to be included when sending information to it. This allows for the transmission of more detailed data. For example, when sending the “Button Clicked” event to Google Analytics, users can include additional information such as the ID of the button element and the content of the `user_id` cookie at the time of the button press. Zaraz offers the flexibility to add as many fields as desired when configuring the action.
**Introducing Worker Variables for Data Manipulation**
To address the need for data manipulation before sending it to third-party tools, Cloudflare recently introduced Worker Variables. With Worker Variables, users can send information to a Cloudflare Worker, perform manipulations on it, and return a modified value. This feature provides immense power and flexibility, enabling users to communicate with their backend server, retrieve data, and leverage JavaScript to perform necessary calculations. With Worker Variables, users have access to a fully-featured Worker, opening up endless possibilities.
**The Integration of JSONata**
However, users have expressed the need for a middle-ground solution when the data manipulation required is minor. In response to this feedback, Cloudflare decided to integrate with JSONata.
**What is JSONata?**
JSONata is a JSON query and transformation language that offers similar features to jq, the command-line JSON processor. JSONata has a more intuitive and easier-to-understand syntax compared to jq. Since JSONata is a JavaScript library, it was straightforward to integrate into Cloudflare Zaraz.
**Utilizing JSONata with Zaraz**
With the tight integration of JSONata and Cloudflare Zaraz, users can leverage JSONata’s capabilities in the fields of all Actions, Triggers, and Variables. JSONata has access to the Zaraz Context, which contains information from the `zaraz.track()` and `zaraz.ecommerce()` calls, as well as automatically gathered data such as the current page URL, cookies, page title, and user-agent string. Incorporating a JSONata query into the field content is as simple as enclosing it within double curly brackets. The JSONata expression, along with the Zaraz Context object, will produce a resulting value for the field.
**Examples of Using JSONata with Zaraz**
Let’s explore two examples of using JSONata with Zaraz. In scenarios where there is a need to convert a string to lowercase, JSONata can be employed. For instance, if the original string is derived from a cookie named `loggedIn` that specifies if the current user is logged in, we can use JSONata to transform the value to lowercase with the expression `$lowercase(system.cookies.loggedIn)`. We can then incorporate this expression within a trigger by navigating to the Zaraz dashboard, choosing the relevant trigger, locating the match rule, and entering `{{ $lowercase(system.cookies.loggedIn) }}` as the value. This will allow comparing the cookie value in its lowercase format.
Simple calculations can also be performed using JSONata. Suppose you are using `zaraz.track()` to send the content of a cart. If you want to include the total sum of all products in a field, you can use the JSONata expression `{{ $sum(client.products.(price * quantity)) }}`. This will multiply the price of each product by its quantity and sum up the total.
**Start Using JSONata Today**
JSONata support is available to all Zaraz users at no cost and is automatically enabled for all websites. By utilizing JSONata, you can send finely tuned data to your providers or APIs with minimal code and zero maintenance for your data infrastructure. If you need any assistance, join our Discord channel for support.
GIPHY App Key not set. Please check settings