Tag Manager: Create Variable
gtm_create_variableCreate one user-defined variable in a draft workspace of a Google Tag Manager container: a constant (e.g. a GA4 measurement id), a data layer variable (e.g. ecommerce.value), a URL part (e.g. the gclid query parameter), a first-party cookie, a global JavaScript variable, or a lookup table mapping one variable's values to outputs. Tags and triggers then reference it as {{Name}}. Saved in the existing workspace the user names (ask which) and NOT published: the user reviews and publishes in Tag Manager. Requires editing to be enabled on the Tag Manager connection. Custom JavaScript variables are not created.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"workspace": "<workspace>",
"name": "<name>",
"type": "<type>"
},
"name": "gtm_create_variable"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | The variable's name, unique in the container, e.g. "DL - Order Value". Tags reference it as {{name}}. | |
| type | Yes | Kind of variable: CONSTANT, DATA_LAYER, URL, COOKIE, JAVASCRIPT_VARIABLE or LOOKUP_TABLE. | |
| notes | No | Notes shown on the variable in Tag Manager. Optional. | |
| value | No | CONSTANT: the constant's value. | |
| container | No | The container: its public id (GTM-XXXXXXX), its name, or its API path from gtm_list_containers. May be left out when the connected login reaches exactly one container. | |
| query_key | No | URL with url_component QUERY: the query parameter read, e.g. gclid. | |
| workspace | Yes | The existing Tag Manager workspace to save the draft in, by name or id, as the user named it (gtm_list_workspaces). Ask the user which workspace to use; never pick one yourself. Nothing is published. | |
| cookie_name | No | COOKIE: the first-party cookie read. | |
| lookup_rows | No | LOOKUP_TABLE: rows as input value → output value, e.g. {"shop.example.com": "G-AAAA", "blog.example.com": "G-BBBB"}. Replaces the whole table on update. | |
| lookup_input | No | LOOKUP_TABLE: the variable whose value is looked up, e.g. Page Hostname (with or without {{ }}). | |
| decode_cookie | No | COOKIE: URI-decode the value. Default false. | |
| default_value | No | DATA_LAYER or LOOKUP_TABLE: the value when the key is absent or no row matches. Optional. | |
| url_component | No | URL: the part of the page URL: URL, PROTOCOL, HOST, PORT, PATH, QUERY, FRAGMENT. | |
| data_layer_key | No | DATA_LAYER: the data layer key read, e.g. ecommerce.value or transactionId. | |
| js_variable_name | No | JAVASCRIPT_VARIABLE: the global JavaScript variable read, e.g. window.dataLayerConfig.value (dot paths allowed). | |
| data_layer_version | No | DATA_LAYER: 1 or 2 (default 2, the one with dot-path keys). |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| kind | Yes | CONSTANT, DATA_LAYER, URL, COOKIE, JAVASCRIPT_VARIABLE or LOOKUP_TABLE. | |
| name | Yes | The variable's name. | |
| type | Yes | Google's type code: c, v, u, k, j or smm. | |
| publicId | No | Public GTM-XXXXXXX id of the container. | |
| published | Yes | Always false: the change is a draft until a version is created and published. | |
| parameters | Yes | The variable's parameters by key, values as text. | |
| variableId | Yes | Id of the new variable inside the container. | |
| workspaceId | Yes | Id of the workspace the change was saved in. | |
| workspaceUrl | Yes | Where the user reviews, previews and publishes the workspace in Tag Manager. | |
| containerName | Yes | The container's name. | |
| containerPath | Yes | API path of the container, accounts/{a}/containers/{c}. | |
| workspaceName | Yes | Name of that workspace. |