DEV Community

maruware
maruware

Posted on

6 1

Setting Google Analytics with react, redux, material-ui etc.

Installation

yarn add react-ga

Code

Initialize

import ReactGA from 'react-ga'
ReactGA.initialize('UA-000000-01')

Bind react-router

export const history = createHistory()
history.listen((location, action) => {
  ReactGA.set({ page: location.pathname })
  ReactGA.pageview(location.pathname)
})

Record material-ui/Popover

import React from 'react'
import ReactGA from 'react-ga'
import Popover from 'material-ui/Popover'

export default ({name, ...rest}) => {
  return (
    <Popover
      onEntered={() => ReactGA.modalview(name)}
      {...rest}
    />
  )
}

Record material-ui/Dialog

import React from 'react'
import ReactGA from 'react-ga'
import Dialog from 'material-ui/Dialog'

export default ({name, ...rest}) => {
  return (
    <Dialog
      onEntered={() => ReactGA.modalview(name)}
      {...rest}
    />
  )
}

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay