Skip to content

Snowflake Query Tags

Composable Audience Studio (CAS) runs segmentation, activation, and supporting queries directly on your Snowflake account, using your warehouse's compute. To keep that usage transparent, Composable Audience Studio sets a structured, JSON-formatted Snowflake QUERY_TAG on the queries it issues. The tag records which application ran the query, which parent segment, segment, or activation it belongs to, and what kind of operation it was — so you can separate Composable Audience Studio workloads from the rest of your Snowflake activity, attribute compute costs to individual audiences, segments, and activations, and set up monitoring or chargeback directly in Snowflake.

Query tagging requires no configuration. Tags appear automatically in your Snowflake query history for the operations listed on this page.

How Query Tagging Works

  • Composable Audience Studio sets the tag on each query individually, as a request-level session parameter. Every tagged query carries the exact context (audience, segment, activation) it ran for.
  • The tag is metadata only. It has no effect on query results, performance, or cost.
  • Your own account-level or user-level QUERY_TAG defaults are not modified. Under Snowflake's parameter precedence, the request-level tag applies only to the sessions Composable Audience Studio opens.
  • Tags are recorded in SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY (365-day retention, up to about 45 minutes of latency) and in the INFORMATION_SCHEMA.QUERY_HISTORY table function (past 7 days, near real time).

Query Tag Schema

The Composable Audience Studio query tag is a JSON object with the following fields:

FieldTypePresentDescription
appstringAlwaysApplication identifier. Fixed value: "TreasureAudienceStudio". Filter on this field to isolate all Composable Audience Studio queries.
query_typestringAlwaysThe operation the query performs. See Query Types.
td_account_idintegerAlwaysThe Treasure AI account ID the query runs for.
audience_idintegerWhen the query belongs to a parent segmentThe parent segment (audience) ID. Omitted for queries that run before the parent segment exists, such as configuration validation.
segment_idintegerSegmentation and activation queriesThe segment ID. Omitted for audience-level queries such as metering.
activation_idintegerActivation queries onlyThe activation ID.

Fields that do not apply to a query are omitted from the JSON rather than set to null, so the tag stays compact and parseable.

Query Types

The query_type field takes one of the following values. These values are stable — you can build dashboards and alerts on them.

ValueDescriptionWhen It Runs
segmentationEvaluates segment rules to identify matching profilesSegment preview, segment builds, profile counts for the segment rule
activationExports the profiles matching a segment to a destinationEvery query in an activation run, including temporary-table creation, the export read, and cleanup
profile_lookupLooks up an individual customer profile and its activityViewing a profile in the Composable Audience Studio UI
validationValidates schema and configurationParent segment and segment validation, including before the parent segment is created
metadataDiscovers schema, lists fields, and fetches sample valuesSegment builder UI, field exploration
meteringCounts profiles for usage meteringScheduled parent segment metering runs

How Activation Runs Are Tagged

One activation run executes several Snowflake queries: creating a temporary table of matching profiles, reading it for export to the destination, and dropping it afterward. All queries in a run — including cleanup after a failure — carry the identical tag, so grouping by activation_id sums the compute cost of the whole run. Scheduled repeats and retries also carry the same tag; distinguish individual runs by query start time.

Activations configured before query tagging became available start carrying tags the next time the activation is updated.

Example Tags

A segmentation query, tagged with its audience and segment:

{
  "app": "TreasureAudienceStudio",
  "audience_id": 111,
  "segment_id": 222,
  "query_type": "segmentation",
  "td_account_id": 12345
}

An activation query additionally carries the activation ID:

{
  "app": "TreasureAudienceStudio",
  "audience_id": 111,
  "segment_id": 222,
  "activation_id": 333,
  "query_type": "activation",
  "td_account_id": 12345
}

Analyze Composable Audience Studio Queries in Snowflake

Because the Composable Audience Studio query tag is JSON, you can parse it in Snowflake with the TRY_PARSE_JSON() function and filter or group on any field.

List recent Composable Audience Studio queries:

SELECT START_TIME, QUERY_TEXT, QUERY_TAG
FROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
WHERE TRY_PARSE_JSON(QUERY_TAG):app::STRING = 'TreasureAudienceStudio'
  AND START_TIME >= DATEADD('day', -7, CURRENT_TIMESTAMP())
ORDER BY START_TIME DESC;

Summarize the last 7 days of Composable Audience Studio activity by query type, audience, and segment:

SELECT
    TRY_PARSE_JSON(QUERY_TAG):query_type::STRING AS query_type,
    TRY_PARSE_JSON(QUERY_TAG):audience_id::INTEGER AS audience_id,
    TRY_PARSE_JSON(QUERY_TAG):segment_id::INTEGER AS segment_id,
    COUNT(*) AS query_count,
    SUM(TOTAL_ELAPSED_TIME) / 1000 AS total_elapsed_seconds,
    SUM(CREDITS_USED_CLOUD_SERVICES) AS total_credits
FROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
WHERE TRY_PARSE_JSON(QUERY_TAG):app::STRING = 'TreasureAudienceStudio'
    AND START_TIME >= DATEADD('day', -7, CURRENT_TIMESTAMP())
GROUP BY query_type, audience_id, segment_id
ORDER BY total_credits DESC;

Attribute compute to individual activations:

SELECT
    TRY_PARSE_JSON(QUERY_TAG):activation_id::INTEGER AS activation_id,
    TRY_PARSE_JSON(QUERY_TAG):audience_id::INTEGER AS audience_id,
    TRY_PARSE_JSON(QUERY_TAG):segment_id::INTEGER AS segment_id,
    COUNT(*) AS query_count,
    SUM(TOTAL_ELAPSED_TIME) / 1000 AS total_elapsed_seconds
FROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
WHERE TRY_PARSE_JSON(QUERY_TAG):app::STRING = 'TreasureAudienceStudio'
    AND TRY_PARSE_JSON(QUERY_TAG):query_type::STRING = 'activation'
    AND START_TIME >= DATEADD('day', -7, CURRENT_TIMESTAMP())
GROUP BY activation_id, audience_id, segment_id
ORDER BY total_elapsed_seconds DESC;

ACCOUNT_USAGE.QUERY_HISTORY can lag by up to about 45 minutes. To verify a query that just ran, use the INFORMATION_SCHEMA.QUERY_HISTORY table function instead, which returns near-real-time results for the past 7 days.

Limitations

  • Query tags are set for Snowflake parent segments only. Queries on Databricks and BigQuery parent segments do not carry tags.
  • The tag schema is fixed and system-defined. Custom fields and values are not supported.

Next Steps