create_variable
Create custom variables in Google Tag Manager to track and store data for tags and triggers. Define variables like constants, data layer values, JavaScript, DOM elements, cookies, URLs, or built-in variables to capture and reuse data across your GTM implementation.
Instructions
新しい変数を作成します
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| accountId | Yes | アカウントID | |
| containerId | Yes | コンテナID | |
| workspaceId | Yes | ワークスペースID | |
| name | Yes | 変数名 | |
| type | Yes | 変数タイプ(例: "c"=定数, "v"=データレイヤー, "j"=JavaScript, "d"=DOM要素, "k"=Cookie, "u"=URL, "ae"=自動イベント, "b"=組み込み変数など) | |
| parameter | No | 変数のパラメータ配列。タイプに応じて必要なパラメータを設定してください。 |
Implementation Reference
- src/gtm-client.js:305-312 (handler)The core handler function that executes the tool logic by calling the Google Tag Manager API to create a new variable in the specified workspace.async createVariable(accountId, containerId, workspaceId, variableData) { await this.ensureAuth(); const response = await this.tagmanager.accounts.containers.workspaces.variables.create({ parent: `accounts/${accountId}/containers/${containerId}/workspaces/${workspaceId}`, requestBody: variableData }); return response.data; }
- src/index.js:730-762 (schema)The input schema definition for the create_variable tool, specifying parameters like accountId, containerId, workspaceId, name, type, and parameter.name: 'create_variable', description: '新しい変数を作成します', inputSchema: { type: 'object', properties: { accountId: { type: 'string', description: 'アカウントID', }, containerId: { type: 'string', description: 'コンテナID', }, workspaceId: { type: 'string', description: 'ワークスペースID', }, name: { type: 'string', description: '変数名', }, type: { type: 'string', description: '変数タイプ(例: "c"=定数, "v"=データレイヤー, "j"=JavaScript, "d"=DOM要素, "k"=Cookie, "u"=URL, "ae"=自動イベント, "b"=組み込み変数など)', }, parameter: { type: 'array', description: '変数のパラメータ配列。タイプに応じて必要なパラメータを設定してください。', }, }, required: ['accountId', 'containerId', 'workspaceId', 'name', 'type'], }, },
- src/index.js:1832-1853 (registration)The registration and dispatch logic in the CallToolRequestSchema handler that maps the tool call to gtmClient.createVariable.case 'create_variable': return { content: [ { type: 'text', text: JSON.stringify( await this.gtmClient.createVariable( args.accountId, args.containerId, args.workspaceId, { name: args.name, type: args.type, parameter: args.parameter || [], } ), null, 2 ), }, ], };
- src/gtm-helpers.js:344-458 (helper)Helper templates for creating common variable configurations (e.g., dataLayer, JavaScript, DOM Element) used to build the parameter array for create_variable.export const VariableTemplates = { // データレイヤー変数 dataLayer: (dataLayerVariable, dataLayerVersion = 2) => ({ type: 'v', parameter: [ { type: 'template', key: 'dataLayerVersion', value: String(dataLayerVersion) }, { type: 'template', key: 'dataLayerVariable', value: dataLayerVariable } ] }), // JavaScript変数 javascript: (javascriptCode) => ({ type: 'j', parameter: [ { type: 'template', key: 'javascript', value: javascriptCode } ] }), // DOM要素変数 domElement: (selector, attributeName = null) => { const params = [ { type: 'template', key: 'selector', value: selector }, { type: 'template', key: 'attributeName', value: attributeName || '' } ]; return { type: 'd', parameter: params }; }, // 1st Party Cookie変数 cookie: (cookieName) => ({ type: 'k', parameter: [ { type: 'template', key: 'cookieName', value: cookieName } ] }), // URL変数 url: (componentType = 1, queryKey = '') => ({ type: 'u', parameter: [ { type: 'integer', key: 'componentType', value: componentType }, { type: 'template', key: 'queryKey', value: queryKey } ] }), // 定数変数 constant: (value) => ({ type: 'c', parameter: [ { type: 'template', key: 'value', value: value } ] }), // 自動イベント変数 autoEvent: (variableType) => ({ type: 'ae', parameter: [ { type: 'template', key: 'variableType', value: variableType } ] }), // 組み込み変数(有効化のみ) builtIn: (variableType) => ({ type: 'b', parameter: [ { type: 'template', key: 'variableType', value: variableType } ] }) };