Community寫作與編輯github.com

CQL 編寫與除錯 (Croct 代理技能)

用於編寫、閱讀、審查或除錯 Croct 的上下文查詢語言 (CQL):受眾條件、`croct.evaluate()` / `useEvaluation` / `<Personalization query>` / Next.js `cql` 標籤查詢、評估 API `query` 主體 (`/client/web/evaluate`、`/external/web/evaluate`) 以及動態內容值 (`"type": "dynamic"`、`"expression"`)。涵蓋上下文變數 (user、session、page、location、campaign、cart、device、browser、context、now/today)、資料型別與轉換、存取器與選擇器 (`'s`、`property X of`、`[1st]`、`[*]`、`[...]`)、測試 (`is`、`matches`、`is like`、`contains either`、`is between`、`is defined`、`is returning`)、量詞 (`some ... satisfies`)、巨集、修飾符、事件表達式 (`user has viewed a page`、`user has abandoned a cart`、`user has completed goal`)、受眾估算與配方。觸發詞:「write an audience」、「target mobile users in New York」、「CQL syntax error」、「audience never matches」、「unavailable estimate」。

CQL 編寫與除錯 (Croct 代理技能) 是什麼?

此技能專為開發者與 AI 代理設計,提供使用 Croct 上下文查詢語言 (CQL) 的完整指南。CQL 是一種類英語的查詢語言,用於定義受眾、執行即時評估以及設定動態內容值。技能涵蓋從基本語法(如屬性存取、邏輯運算與比較)到進階概念(如事件表達式、量詞與巨集)的所有內容。它支援多種平台,包括 Web SDK (`croct.evaluate()`)、React (`useEvaluation`)、Next.js (伺服器端的 `cql` 標籤) 以及 HTTP API(例如 `/client/web/evaluate`)。主要功能包含快速的速查表、常見陷阱的除錯清單、按使用案例分類的配方(如活動、地理位置、個人檔案),以及指向官方文件的參考連結。開發者若想快速編寫、測試及除錯 CQL 查詢,以實現精確的個人化與目標定位,此技能將是不可或缺的工具。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/croct-tech/agent-skills/tree/HEAD/skills/croct-cql

Installed? Explore more 寫作與編輯 skills: steipete/notion, langchain-ai/langchain, bytedance/podcast-generation · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

編寫與除錯 CQL

CQL 是 Croct 的類英語查詢語言。相同的表達式語法可支援受眾、來自 SDK 與 HTTP API 的即時評估,以及動態內容值。此處所有內容均來自 docs.croct.com;當有內容未涵蓋時,請查閱線上文檔(位於此檔案底部),切勿自行猜測。

工作流程

  1. 識別查詢的執行位置:受眾(必須為布林值)、evaluate/API(任何 JSON 值,最多 500 個字元)或動態值(結果必須符合屬性型別,否則使用 default)。
  2. 選擇資料來源:當前上下文(page、cart、location、campaign、device)、儲存的個人檔案(user's ...、user's stats)、工作階段(session's ...)或歷史記錄(user has ... 事件表達式,Scale 方案)。
  3. 檢查資料是否已收集:任何標記為 需要整合 的內容(個人檔案屬性、購物車、興趣、目標、登入狀態)在應用程式尚未發送資料前,永遠不會匹配。
  4. 使用下方的速查表進行編寫;如需查詢確切屬性,請參閱 context-and-data-types.md。
  5. 執行陷阱檢查清單,然後使用 croct.evaluate() 或儀表板估算工具進行測試。

速查表

// 自然、技術與混合風格均可互換
user's score is greater than 90
user.score > 90
user's score > 90
需求語法
屬性user's address's city、user's stats' orders、property city of user's address、user.address.city
特殊鍵值 / 索引page.query['coupon-code']、list[0]、(user's interests)[0]
選擇器cart.items[1st]、[:first]、[:last(2)]、items[*].price、data[...].price
字面值null、true、42、3.14 (十進位)、3.14f、51%、"str"、'raw'、~/re/i、[1, 2]、["k": "v"]
日期無字面值:"2026-01-01" as date、"2026-01-01T12:00" as datetime;today + 1 day
邏輯and or not (&& || !);and 的綁定優先級高於 or
比較== != > >= < <=;1 < x < 3;is equal to X ignoring case
字面值簡寫X is "value"、X is not 0(右側必須是字面值)
字串starts with、ends with、matches "sub" / matches ~/re/i (區分大小寫)、is like "sub" (不區分大小寫)
集合contains X、X is in [..]、contains all of [..]、contains either [..]、contains neither [..]
存在性is defined / is known、is empty
使用者/工作階段user is returning、user is anonymous、user is identified、session is starting
日期is before、is after、is at "12:00"、is between A and B [exclusive]、today's weekday is "monday"
量詞some x in C satisfies P、every、no、not every、at least 2 of any [distinct] x in C satisfies P
巨集count of、sum of、average of、min of、max of、first of、last of、keys of、length of、number of days between A and B、this month
修飾符as number、rounded [down]、in title case、sorted descending、without duplicates、replacing "a" with "b"、today at "12:00"
備用值user's name or else "Anonymous" (??)
條件式"A" if cond otherwise "B"、cond ? "A" : "B"
變數let orders = user's stats' orders; orders >= 5
事件 (Scale)user has <event> [with prop value] [at least N times] [in last 7 days]
註解// line、# line、/* block */

關鍵字與識別碼不區分大小寫;字串字面值則區分大小寫。

上下文速覽

變數常用屬性
userid、firstName、email、age、company、jobTitle、interests、activities、stats' sessions、stats' orders、任何自訂屬性
sessionstart、duration、landingPage、referrer、stats' pageviews、stats' orders、audiences、experiences、experiments、自訂屬性
page (網頁)title、url、path、query、anchor、referrer
locationcontinentCode、countryCode、countryName、stateCode/regionCode (state's code 的別名)、stateName、cityName、timeZone、tags
device / browsercategory (desktop、tablet、mobile、bot、...)、vendor、name、os;browser's name、type
campaignname、source、medium、content、term (UTM)
cartstage (browsing、shopping、checkout)、items、total、subtotal、coupon、discount
contexttimeZone、在評估時傳入的屬性
now、today、yesterday、tomorrowweekday、day、month、year、hour...

最有用的模式

user is returning
user is not returning
session is starting
device's category is "mobile"
device's category is not "bot"
location's countryCode is "US"
location's state's code is in ["NY", "CA"]
location's cityName is "New York" and today's weekday is "monday"
campaign's source is "google" and campaign's medium is "cpc"
campaign's name matches "sale"
page's path is in ["/pricing", "/plans"]
page's query's newsletter is "daily-news"
page.query['coupon-code'] is defined
session's referrer matches "google"
session's landingPage starts with "https://example.com/promo"
session's duration > 5 minutes and session's stats' orders is 0
user's interests contains either ["marketing", "sales"]
user's age is between 18 and 30
user's stats' orders is 0
user's plan is "premium"
cart's stage is "checkout" and cart's total is greater than 100
some item in cart's items satisfies item's brand is "Apple"
(no item in cart's items satisfies item's name is like "smartphone") and user is returning
user has abandoned a cart at least 2 times
user has viewed a product with category "Shoes" in last 7 days
user has completed goal "demo-requested" last session
user's firstName or else "there"

按使用案例(活動、地理位置、技術、工作階段、個人檔案、購物車、訂單、值查詢)查看更多:references/recipes.md。

從程式碼評估

import croct from '@croct/plug';

const isReturning = await croct.evaluate<boolean>('user is returning').catch(() => false);
const inList = await croct.evaluate<boolean>(
  "context's countries include location's countryName",
  {attributes: {countries: ['United States', 'Canada']}}, // 讀取為 `context's countries`
);

Next.js 伺服器端:使用 @croct/plug-next/server 中的 await evaluate(query) 或 await cql`user's persona is in ${roles}` 。React:使用 useEvaluation(query)。HTTP:POST https://api.croct.io/client/web/evaluate,主體為 {"query": "..."}。詳細資訊、屬性限制、動態值與估算,請參閱:references/evaluation.md。

陷阱與除錯

症狀可能原因修正
x is y 語法錯誤is 簡寫需要右側為字面值使用 x is equal to y 或 x == y
location city is "Berlin" 語法錯誤缺少存取器:變數與其屬性之間的裸詞使用 location's cityName、property cityName of location 或 location.cityName
比較 "1" > 0 或 session's start > "2026-01-01" 時出錯關係運算子絕不會轉換型別進行轉換:"1" as number > 0、... > "2026-01-01" as datetime,或使用 is after
地理位置/代碼永不匹配大小寫錯誤:代碼為大寫 ("US"、"NA"、"CA") 且字串區分大小寫使用大寫、is like、is equal to ... ignoring case 或 ~/.../i
matches "Sale" 錯過 "sale"matches、starts with、ends with 區分大小寫使用 is like "sale" 或 matches ~/sale/i
量詞吞掉後續條件量詞謂語延伸至結尾將整個量詞用括號括起來
a or b and c 行為異常and 的綁定優先級高於 or加上括號
條件結果被錯誤相乘條件式是貪婪的(A if c otherwise B) * price
個人檔案/購物車/興趣條件永不成立需要整合的資料尚未發送追蹤事件或設定屬性(請參閱 croct-track-events 技能)
X is greater than 30 和 X is not greater than 30 都為 false值未定義:布林錯誤容錯產生 false使用 X is defined and ... 或 X or else 0
user is identified 總是 false未追蹤登入/登出事件追蹤它們;否則使用者永遠是匿名
cart.items[2nd]、first of、average of、min of 錯誤缺少元素或空集合附加 or else null / or else 0
contains none of [] 對比 contains neither []空右側:none 為 false,neither 為 true對於「無重疊」使用 contains neither
混合 's 與括號時出錯所有格/屬有格加上括號需要括號使用 (cart's items)[1st] 或點記法 cart.items[*]
事件表達式不可用它們是 Scale 方案的功能在可能的情況下使用個人檔案/工作階段統計資料 (user's stats' orders)
「無法使用估算」使用了 page、當前上下文或 now/today/yesterday這是預期行為;受眾在執行時期仍可正常運作
範圍錯誤.. 範圍最多 30 個項目縮小範圍
變數/參數名稱錯誤變數無法重新定義;名稱必須是識別碼重新命名;避免使用內建名稱 (user、page) 和通用詞彙

應做事項:

  • 在與非開發人員共享的受眾中,優先使用明確的 is equal to、is greater than;當技術風格讀起來更順時再使用它。
  • 使用 is defined 或 or else 保護可選資料。
  • 在 and 鏈中將計算成本低的條件放在前面(短路求值會跳過其餘條件)。
  • 使用 contains either / contains neither,而不是否定 contains all / contains none。

不應事項:

  • 不要發明測試、變數或事件措辭。例如 user is new 或 user has made purchase 等範例出現在文件敘述中,但並非有記錄的結構;請使用 user is not returning 和 user's stats' orders is not 0(或 user has placed an order)。
  • 不要將 location's countryName 與代碼進行比較;對於 "US" 請使用 countryCode。
  • 不要在算術中依賴隱式的日期轉換:"2021-01-01" as date + 1 day。

參考資料

檔案何時閱讀
references/syntax.md詞法規則、let、函式、字面值、存取器與選擇器、優先級表、條件式、持續時間
references/context-and-data-types.md每個變數與屬性、可用性標記、列舉(裝置類別、購物車階段、洲別、位置標籤)、轉換
references/expressions.md所有測試、運算子、錯誤容錯、量詞、巨集、修飾符
references/events.md事件表達式、條件運算子、計數量詞、時間視窗、追蹤需求
references/evaluation.mdSDK evaluate / cql / hooks、評估 API、上下文屬性、動態值、受眾估算
references/recipes.md按使用案例分類的即用受眾與值查詢

相關技能:croct-integrate-sdk (SDK 設定)、croct-track-events (發送查詢讀取的事件與屬性)、croct-use-api (HTTP API)、croct-model-content (內容定義中的動態值)、croct-personalize-and-test (使用受眾的體驗與實驗)、croct-use-croct (概念與導覽)。

線上文檔

Individual skills in this repo

This repo contains 4 individual skills — each has its own dedicated page.

croct-tech/agent-skills

>- Use when connecting Croct to a third-party platform or migrating to Croct from another personalization or experimentation tool. Covers Storyblok (Optimize by Croct app, @croct/plug-storyblok, Storyblok withCroct, resolveContent, croct/plug-storyblok, CroctStoriesApi), Sanity and Strapi (slot fallback, dynamic zones, metadata.contentSource), Shopify Hydrogen storefront events, Shopify custom pixel (croctPixel, PUBLIC_CROCT_SHOPIFY_PIXEL, checkout and order tracking), VTEX, Google Tag Manager data layer, Microsoft Clarity tags, HubSpot forms (hbspt.forms.create, hs-form-event), firmographic providers 6sense, Demandbase, Clearbit Reveal, and ZoomInfo WebSights (firmographics attributes, account-based audiences in CQL), and migrations from Mutiny and Ninetailed (Experience component, Contentful sunset). Triggers include cdn.croct.io/plug-integration scripts, "personalize Storyblok blocks", "track Shopify checkout", "send VTEX events", "ABM personalization", "replace Mutiny".

croct-tech/agent-skills

>- Use when installing or using a Croct SDK in application code: rendering slot content, evaluating CQL queries, server or client fetching, prerendering, identifying users, app ID / API key env vars, testing, or debugging. Covers JavaScript (`@croct/plug`, `croct.fetch`, `croct.evaluate`), React (`@croct/plug-react`, `<CroctProvider>`, `useContent`, `<Slot>`, `useEvaluation`, `useCroct`), Next.js (`@croct/plug-next`, `fetchContent`, `cql`, middleware/proxy, `NEXT_PUBLIC_CROCT_APP_ID`, `CROCT_API_KEY`), Vue (`@croct/plug-vue`), Nuxt (`@croct/plug-nuxt`), Shopify Hydrogen (`@croct/plug-hydrogen`), PHP (`croct/plug-php`), Laravel, Symfony, and Drupal. Triggers: slot IDs like `home-hero@1`, `SlotContent`, "Croct is not plugged in", "initial content is required for server-side rendering", "Did you forget to configure Croct's middleware?", CORS errors.

croct-tech/agent-skills

>- Use when designing or changing Croct content models: regular vs union components, what becomes a component, slot, structure, or primitive attribute; writing component schema JSON (`structure`, `union`, `reference`, `text`, `number`, `boolean`, `list`, `@croct/file`, `@croct/richText` rich text, `format`, `choices`, `optional`, `private`, `position`); writing content definition JSON (`static`/`dynamic` values, `nullable`, `default`, `{{placeholder}}` paths); slot default, localized, experience, experiment, or content group content; schema limit errors (30 attributes, 6 nesting levels, 10 union members, complexity score 50); component versioning and breaking changes (`home-hero@2`); refactoring (duplicate and modify, duplicate and replace, flatten and merge); Markdown in text attributes with `@croct/md-lite`. Phrasings: "model content for a hero", "add an attribute to the banner component", "support variations", "component is too complex", "localize the hero", "translations", `preferredLocale`.

croct-tech/agent-skills

>- Use when planning or implementing personalization or A/B testing with Croct: creating an experience (audience, slots, content, priority, scheduling, drafts), adding an experiment (A/B/n, primary goal ID, traffic allocation, variants, cross-device consistency), wiring the code it needs (slots, `croct.track('goalCompleted', {goalId})`, `orderPlaced`, `leadGenerated`, `userSignedUp`), server-side feature-flag experiments, redirect experiments, personalization recipes (UTM campaigns, ABM, search terms, location, free shipping, interests, blog readers, leads, customers, organic intent, HubSpot forms), or reading the experience, experiment, application, or workspace dashboards (probability to be best, potential loss, "Experiment in progress", "Recommended winner", "No clear winner", "Track your goal"). Phrasings: "personalize the hero for LinkedIn visitors", "A/B test this CTA", "which variant won?", "why does my experiment show no conversions?".

相關技能