You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

非开发者求助:如何在Craft CRM中通过Tag Manager插入HTML代码

Fixing the "Unknown variable 'craft.googleTagManager.headSection()'" Error in Craft CRM & Google Tag Manager

Hey there! No need to stress at all—this is a super common mix-up when you're new to working with Craft CRM and Google Tag Manager (GTM). Let me break down exactly what's going wrong and how to fix it:

What's the Root Issue?

The code snippets {{ craft.googleTagManager.headSection() }} and {{ craft.googleTagManager.bodySection() }} aren't meant to be pasted into Google Tag Manager itself. These are Craft CMS template commands—they only work inside your website's Craft template files, not in GTM's custom HTML tags. GTM doesn't understand Craft's Twig template syntax (the double curly braces), which is why you're getting that "unknown variable" error.

Step-by-Step Solution

Here's what you need to do instead:

  1. Find your Craft site's main layout template
    This is usually a file named something like _layout.twig in your site's templates folder (you can access this via your website's file manager or FTP). This file controls the basic structure of every page on your site.
  2. Add the head code to your <head> tag
    Open the layout file, locate the opening <head> tag, and paste {{ craft.googleTagManager.headSection() }} as high inside the <head> as possible (right after meta tags like <meta charset="UTF-8"> is perfect).
  3. Add the body code right after <body>
    Find the opening <body> tag in the same layout file, and paste {{ craft.googleTagManager.bodySection() }} immediately after it—no extra spaces required.
  4. Configure the GTM plugin in Craft
    Head back to your Craft CRM admin panel, open the Google Tag Manager plugin settings, and enter your GTM container ID (it looks like GTM-XXXXXXX). Save these settings.
  5. Use GTM normally from here
    You don't need to paste any Craft-specific code into GTM anymore. The plugin will automatically turn those template commands into the official GTM container code that your site needs. Now you can create tags, triggers, and variables in GTM just like you would for any other site.

Quick Recap

  • Craft's template code stays in your site's template files, not GTM
  • The plugin handles converting those craft.googleTagManager commands into working GTM code for your site
  • GTM is for setting up your tracking tags after the container code is live on your site

内容的提问来源于stack exchange,提问作者Faraz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:36:40