Cloudflare 中文文档
Zaraz
编辑这个页面
跳转官方原文档
Set theme to dark (⇧+D)

Context Enricher

The Zaraz Context Enricher is a tool to modify or enrich the context that is being used across Zaraz using a Cloudflare Worker. The Context Enricher allows you access to the client and system variables.

​​ Creating a Worker

To use a Context Enricher, you first need to create a new Cloudflare Worker. You can do this through the Cloudflare dashboard or by using Wrangler.

To create a new Worker in the Cloudflare dashboard:

  1. Log in to the Cloudflare dashboard.
  2. Go to Workers & Pages and select Create application.
  3. Give a name to your Worker and select Deploy.
  4. Select Edit code.

You have now created a basic Worker that responds with “Hello world.” To make this Worker functional when using it as a Context Enricher, you need to change the code to return the context back:

Keep reading for more complete examples of different use cases or refer to Zaraz Context.

​​ Configuring your Context Enricher

Now that your Worker is published, you can select it in your Zaraz settings:

  1. Log in to the Cloudflare dashboard.
  2. Go to Zaraz > Settings.
  3. Select your Context Enricher Worker.
  4. Save your settings.

Your Context Enricher will now run on all Zaraz requests in that given zone.

​​ Example Context Enricher

​​ Adding arbitrary information using an API

You can use the Context Enricher to add information to your context. For example, you could use an API to get the current weather for the user’s location and add it to the context.

Now, you can use the weather property anywhere in Zaraz by choosing the Track Property from the attributes input and entering weather.

​​ Masking sensitive information, such as emails

Let’s assume we want to redact sensitive information, such as emails. For this, we’re going to replace all occurrences of email addresses throughout the context. Please keep in mind that this is only an example and might not fit all edge or use cases.

For the sake of simplicity of this example, we’re going to replace all strings that contain an @ symbol: