1.0.0 • Published 2 years ago

google-analytics-next v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

��# google-analytics-next

Usage

Add the enable call in your _app.js.

If your App is a class:

import React from "react";
import { enableGoogleAnalytics } from '@/components/google-analytics/google-analytics';


class MyApp extends App {

  componentDidMount() {
    enableGoogleAnalytics(process.env.GOOGLE_ANALYTICS_ID);
  }

  
  render() {
    const { Component, pageProps } = this.props;
    return <Component {...pageProps} />;
  }
}

export default MyApp;

If you App is a functional component:

import React, { useEffect } from "react";
import App from "next/app";

import { enableGoogleAnalytics } from '@/components/google-analytics/google-analytics';

function MyApp({ Component, pageProps }) {
  useEffect(() => {
    enableGoogleAnalytics(process.env.GOOGLE_ANALYTICS_ID);
  }, []);
  
  return <Component {...pageProps} />;
}

export default MyApp;

It will track routes changes by default.