@rollin
To include Google Analytics in a Preact application, you can follow these steps:
1
|
npm install react-ga |
1 2 3 4 5 6 7 8 9 10 |
import ReactGA from 'react-ga'; export const initGA = () => { ReactGA.initialize('UA-XXXXXXXXX-X'); }; export const logPageView = () => { ReactGA.set({ page: window.location.pathname }); ReactGA.pageview(window.location.pathname); }; |
Replace 'UA-XXXXXXXXX-X'
with your actual tracking ID.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import { Component } from 'preact'; import { initGA, logPageView } from './analytics.js'; class App extends Component { componentDidMount() { initGA(); logPageView(); } render() { return <div>Your Preact App</div>; } } export default App; |
That's it! Google Analytics tracking should now be included in your Preact application. Remember to test it by navigating to different pages and checking the Google Analytics dashboard for data.