Skip to content

React Context and hooks for integrating Google Analytics 4 into an application

Notifications You must be signed in to change notification settings

lancetipton/rga4

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RGA4

React context and hooks for integrating Google Analytics 4 into an application

Add / Install

  yarn add @keg-hub/rga4
  npm install @keg-hub/rga4

Setup

Important

Example

  import React, { useEffect, useCallback } from 'react'
  import { RGA4Provider, useRGA4 } from '@keg-hub/rga4'

  const Child = (props) => {

    // Use the hook to get access to the Google Analytics Context
    const rga4 = useRGA4()
    
    useEffect(() => {
      // Call the rga4.event method to send an analytics event
      rga4.event('page_view', {
        label: 'Github Readme',
        category: 'engagement',
      })
    }, [])

    // Custom analytics event when a button is clicked
    const onClick = useCallback(() => {
      // Call the rga4.event method to send an analytics event
      rga4.event('button_click', {
        event_label: 'Demo Button',
        event_category: 'engagement',
      })
    }, [ rga4 ])

    return (
      <div>
        <button onClick={onClick}>
          Demo Analytics Event
        </button>
      </div>
    )
  }

  export const Component = (props) => {
    const measurementID = `Replace with your GA4 Measurement ID`
    
    return (
      <RGA4Provider
        code={measurementID}
        config={{ ...Optional gtag config object }}
        gaCodes={[ ...Optional extra analytics IDs ]}
      >
        <Child />
      </RGA4Provider>
    )
  }

About

React Context and hooks for integrating Google Analytics 4 into an application

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published