[英]Gatsbyjs + Google analytics - tracking custom events?
是否可以使用 gatsby 和 google 分析跟踪自定义事件?
I've used ReactGA in conjunction with Gatsby and had good success.我已经将ReactGA与 Gatsby 结合使用并取得了很好的成功。
For basic event tracking - like logging a clicked link - it's very easy to use.对于基本的事件跟踪——比如记录点击的链接——它非常易于使用。 You create a logging function in your component that accesses
ReactGA.event
and then invoke it in your render function using onClick
.您在访问
ReactGA.event
的组件中创建一个日志记录函数,然后使用onClick
在渲染函数中调用它。
Example component logging a PDF download:记录 PDF 下载的示例组件:
import React from 'react'
import Link from 'gatsby-link'
import ReactGA from 'react-ga'
import logo from './logo.png'
import financials from './Annual_Report_Financials_2017_FINAL.pdf'
class LoggingDownload extends React.Component {
logger() {
// Detect each click of the financial PDF
ReactGA.event({
category: 'Financial Download',
action: 'User clicked link to view financials'
})
}
render() {
return (
<div>
<nav className="nav-container">
<Link to="/locations">
<img className="logo" src={logo} alt="Logo" />
</Link>
<ul className="nav-item-container">
<li className="nav-item">
<a href="/shortsignup/" target="_blank">Join Us</a>
</li>
<li className="nav-item">
<a href={financials} onClick={this.logger} target="_blank" id="financials-pdf">Financials</a>
</li>
</ul>
</nav>
</div>
)
}
}
export default LoggingDownload
There are tons more use cases - check out ReactGA docs.还有更多用例 - 查看 ReactGA 文档。
Also: don't forget to include ggatsby-plugin-google-analytics
in your gatsby-config.js
file as a dependency to getting the above to work correctly:另外:不要忘记在
gatsby-config.js
文件中包含ggatsby-plugin-google-analytics
作为使上述内容正常工作的依赖项:
{
resolve: `gatsby-plugin-google-analytics`,
options: {
trackingId: "UA-#########-##",
// Puts tracking script in the head instead of the body
head: false,
// Setting this parameter is optional
respectDNT: true,
}
}
For anyone who is still wondering, gatsby-plugin-google-analytics
is not the approach I would take for google analytics.对于仍然想知道的人来说,
gatsby-plugin-google-analytics
不是我用于 google 分析的方法。 What you are looking for is gatsby-plugin-google-gtag .您正在寻找的是gatsby-plugin-google-gtag 。 This plugin automatically sends pageviews, as well as makes the
gtag
event available on the window.此插件会自动发送综合浏览量,并使
gtag
事件在窗口中可用。
window.gtag("event", "click", { ...data })
Google also gives us migration docs for anyone who is still using gatsby-plugin-google-analytics
you can find here (link also in the gatsby docs). Google 还为仍在使用
gatsby-plugin-google-analytics
任何人提供迁移文档,您可以在此处找到(链接也在 gatsby 文档中)。
You can use the provided OutboundLink for simple link tracking:您可以使用提供的 OutboundLink 进行简单的链接跟踪:
import React from "react"
import { OutboundLink } from "gatsby-plugin-google-gtag"
export default () => (
<div>
<OutboundLink href="https://www.gatsbyjs.org/packages/gatsby-plugin-google-gtag/">
Visit the Google Global Site Tag plugin page!
</OutboundLink>
</div>
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.