是否可以通过gatsby和Google Analytics(分析)跟踪自定义事件?
我已经将ReactGA与Gatsby结合使用,并取得了良好的成功。
对于基本的事件跟踪(如记录单击的链接),它非常易于使用。您可以在要访问的组件中创建一个日志记录函数,ReactGA.event
然后使用来在呈现函数中调用它onClick
。
记录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
还有更多的用例-查看ReactGA文档。
另外:不要忘记将文件包含ggatsby-plugin-google-analytics
在gatsby-config.js
文件中,以确保上述文件正常运行:
{
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,
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句