I'm using gatsby to develop a website.
I want to use AtInternet to track my page views.
In their docs, they explain how to integrate the tracker with a React Website.
So first, I import the smarttag.js from their cdn in my html.js: import React from "react" import PropTypes from "prop-types" import { withPrefix } from 'gatsby'
export default class HTML extends React.Component {
render() {
return (
<html {...this.props.htmlAttributes}>
<head>
<meta charSet="utf-8" />
<meta httpEquiv="x-ua-compatible" content="ie=edge" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
{this.props.headComponents}
<script type="text/javascript" src="//tag.aticdn.net/XXXXX/smarttag.js"></script>
</head>
<body {...this.props.bodyAttributes}>
{this.props.preBodyComponents}
<div
key={`body`}
id="___gatsby"
dangerouslySetInnerHTML={{ __html: this.props.body }}
/>
{this.props.postBodyComponents}
</body>
</html>
)
}
}
HTML.propTypes = {
htmlAttributes: PropTypes.object,
headComponents: PropTypes.array,
bodyAttributes: PropTypes.object,
preBodyComponents: PropTypes.array,
body: PropTypes.string,
postBodyComponents: PropTypes.array,
}
Note, that I tried to directly use the "classic" tracking script there, but it wouldn't push a new event when a user would navigate to a new page
Then, I create the Service in components/ATInternetService.js :
class ATInternetService {
constructor() {
this.tag = this.newTag({secure: true});
}
newTag() {
try {
return new ATInternet.Tracker.Tag();
} catch(ex) {
return {
dispatch: () => ({}),
page: { set: () => ({}) }
};
}
}
//@param info: {name: string, level2?: string, chapter1?: string, chapter2?: string, chapter3?: string, customObject?: any}
sendPage(name) {
this.tag.page.set(name);
this.tag.dispatch();
}
}
export let tag = new ATInternetService();
Then, I create the component in /components/pushAT.js :
import React from 'react'
import {tag} from '../components/ATInternetService'
class PushAT extends React.Component {
componentDidMount () {
tag.sendPage({
name: '',
})
}
componentDidUpdate (prevProps) {
// Figure out if the path changed
if (this.props.path !== prevProps.path) {
tag.sendPage({
name: '',
})
}
}
}
export default PushAT
And when I use the component on my page, it says :
error 'ATInternet' is not defined no-undef
I used the componentDidMount and ComponentDidUpdate after reading this : Remount componentDidMount() by path.name
I think one of the easiest way to implement the tracker would be just pushing the script on each page change. But I'm still a beginner, and I'm not sure how to do it.
What would you recommend, please?