Javascript 将gtag事件片段放在Gatsby/React站点的头部
在我的感谢页面中放置事件片段时遇到问题。该网站是建立在盖茨比。我的想法是将事件片段放在使用React头盔中,但似乎不知道如何正确执行。我尝试了所有不同的方法,但似乎都不管用:Javascript 将gtag事件片段放在Gatsby/React站点的头部,javascript,reactjs,gatsby,gtag.js,react-helmet,Javascript,Reactjs,Gatsby,Gtag.js,React Helmet,在我的感谢页面中放置事件片段时遇到问题。该网站是建立在盖茨比。我的想法是将事件片段放在使用React头盔中,但似乎不知道如何正确执行。我尝试了所有不同的方法,但似乎都不管用: <Helmet> <script> gtag('event', 'conversion', {'send_to': 'AW-MYID'}); </script> </Helmet> gtag('event','conversion',{'send_to':'AW-MY
<Helmet>
<script>
gtag('event', 'conversion', {'send_to': 'AW-MYID'});
</script>
</Helmet>
gtag('event','conversion',{'send_to':'AW-MYID'});
{`
gtag('event','conversion',{'send_to':'AW-MYID'});
`}
根据文档在服务器端渲染(SSR)中添加自定义事件:
您需要首先访问窗口
对象。当然,在检查是否可用后,请求时间将作为
当然,这假设您已经在您的gatsby config.js
中正确安装了插件。理想的定制应该如下所示:
plugins: [
{
resolve: `gatsby-plugin-google-gtag`,
options: {
// You can add multiple tracking ids and a pageview event will be fired for all of them.
trackingIds: [
"GA-TRACKING_ID", // Google Analytics / GA
"AW-CONVERSION_ID", // Google Ads / Adwords / AW
"DC-FLOODIGHT_ID", // Marketing Platform advertising products (Display & Video 360, Search Ads 360, and Campaign Manager)
],
// This object gets passed directly to the gtag config command
// This config will be shared across all trackingIds
gtagConfig: {
optimize_id: "OPT_CONTAINER_ID",
anonymize_ip: true,
cookie_expires: 0,
},
// This object is used for configuration specific to this plugin
pluginConfig: {
// Puts tracking script in the head instead of the body
head: false,
// Setting this parameter is also optional
respectDNT: true,
// Avoids sending pageview hits from custom paths
exclude: ["/preview/**", "/do-not-track/me/too/"],
},
},
},
],
这是有道理的!非常感谢,费伦!仍然不太确定在哪里添加窗口对象tho。是否应该将其添加到我的感谢页面上的React头盔中?只要在需要触发或更新分析时将其作为函数调用即可。在您的感谢页面中,应该在
componentDidMount
或useffect
hook.hmm中触发。。。它给了我一个错误:“React Hook”useffect在函数“landingPage”中被调用,该函数既不是React函数组件,也不是定制的React Hook函数React hooks/rules of hooks“Yes!”-盖茨比插件google gtag已经安装并运行。解决方案非常简单:必须大写landingPage(组件名称)的第一个字母,这解决了问题!
<Helmet>
<script>
{`
gtag('event', 'conversion', {'send_to': 'AW-MYID'});
`}
</script>
</Helmet>
typeof window !== "undefined" && window.gtag("event", "click", { ...data })
plugins: [
{
resolve: `gatsby-plugin-google-gtag`,
options: {
// You can add multiple tracking ids and a pageview event will be fired for all of them.
trackingIds: [
"GA-TRACKING_ID", // Google Analytics / GA
"AW-CONVERSION_ID", // Google Ads / Adwords / AW
"DC-FLOODIGHT_ID", // Marketing Platform advertising products (Display & Video 360, Search Ads 360, and Campaign Manager)
],
// This object gets passed directly to the gtag config command
// This config will be shared across all trackingIds
gtagConfig: {
optimize_id: "OPT_CONTAINER_ID",
anonymize_ip: true,
cookie_expires: 0,
},
// This object is used for configuration specific to this plugin
pluginConfig: {
// Puts tracking script in the head instead of the body
head: false,
// Setting this parameter is also optional
respectDNT: true,
// Avoids sending pageview hits from custom paths
exclude: ["/preview/**", "/do-not-track/me/too/"],
},
},
},
],