如何将 gatsby-plugin-google-gtag 与 Gatsby.js 一起使用?
How use gatsby-plugin-google-gtag with Gatsby.js?
我刚刚在我的 gatsby-config.js
文件中安装了 gatsby-plugin-google-gtag
:
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-google-gtag`,
options: {
trackingIds: [
"UA-XXXXXXXXX-X", // Google Analytics / GA
"AW-XXXXXXXXX" // Google Ads / Adwords / AW
],
pluginConfig: {
head: true
},
}
}
]
}
然后我将这个事件添加到我的表单中,它应该有效吗?
class Form extends Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
window.gtag("conversion", "click", { send_to: ["AW-XXXXXXXXX/-XXXXXXXXXXXXXXXX"]})
}
render() {
return (
<Div className='au'>
<form action="https://formspree.io/my@emailaddress.io" method="POST">
<InputName type="name" name="name" placeholder="Your Name"/>
<InputMail type="email" name="email" placeholder="Your Mail"/>
<Button type="submit" onClick={this.handleClick}>Contact us</Button>
</form>
</Div>
)
}
}
export default Form;
来自文档:
This plugin only works in production mode!
你的配置看起来也不错,只要确保你的 trackingIds 是正确的
handleClick
应该是:
handleClick() {
window.gtag("event", "conversion", { send_to: ["AW-XXXXXXXXX/-XXXXXXXXXXXXXXXX"]})
}
gtag
格式为:
gtag("event", "<event_name>", {<event_params>});
关于 gtag
here 的额外信息。
我刚刚在我的 gatsby-config.js
文件中安装了 gatsby-plugin-google-gtag
:
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-google-gtag`,
options: {
trackingIds: [
"UA-XXXXXXXXX-X", // Google Analytics / GA
"AW-XXXXXXXXX" // Google Ads / Adwords / AW
],
pluginConfig: {
head: true
},
}
}
]
}
然后我将这个事件添加到我的表单中,它应该有效吗?
class Form extends Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
window.gtag("conversion", "click", { send_to: ["AW-XXXXXXXXX/-XXXXXXXXXXXXXXXX"]})
}
render() {
return (
<Div className='au'>
<form action="https://formspree.io/my@emailaddress.io" method="POST">
<InputName type="name" name="name" placeholder="Your Name"/>
<InputMail type="email" name="email" placeholder="Your Mail"/>
<Button type="submit" onClick={this.handleClick}>Contact us</Button>
</form>
</Div>
)
}
}
export default Form;
来自文档:
This plugin only works in production mode!
你的配置看起来也不错,只要确保你的 trackingIds 是正确的
handleClick
应该是:
handleClick() {
window.gtag("event", "conversion", { send_to: ["AW-XXXXXXXXX/-XXXXXXXXXXXXXXXX"]})
}
gtag
格式为:
gtag("event", "<event_name>", {<event_params>});
关于 gtag
here 的额外信息。