Skip to content

Instantly share code, notes, and snippets.

@alsheuski
Last active March 5, 2025 06:26
Show Gist options
  • Save alsheuski/9c45516449c41246c39c1be6585d5294 to your computer and use it in GitHub Desktop.
Save alsheuski/9c45516449c41246c39c1be6585d5294 to your computer and use it in GitHub Desktop.
Amiplitude provider for integrate and using amplitude in react project properly
import React, { createContext, useContext, useEffect } from 'react'
import * as amplitude from '@amplitude/analytics-browser'
interface AmplitudeContextType {
track: (eventName: string, eventProperties?: Record<string, any>) => void
identify: (userId: string) => void
}
const AmplitudeContext = createContext<AmplitudeContextType | undefined>(undefined)
export interface AmplitudeProviderProps {
children: React.ReactNode
apiKey: string
}
export const AmplitudeProvider: React.FC<AmplitudeProviderProps> = ({ children, apiKey }) => {
useEffect(() => {
if (!apiKey) return
amplitude.init(apiKey, undefined, {
defaultTracking: {
sessions: true,
pageViews: true,
formInteractions: true,
fileDownloads: true,
},
fetchRemoteConfig: true,
autocapture: true,
serverZone: amplitude.Types.ServerZone.EU,
})
}, [apiKey])
const track = (eventName: string, eventProperties?: Record<string, any>) => {
if (apiKey) {
amplitude.track(eventName, eventProperties)
}
}
const identify = (userId: string) => {
amplitude.setUserId(userId)
}
return (
<AmplitudeContext.Provider value={{ track, identify }}>{children}</AmplitudeContext.Provider>
)
}
export const useAmplitude = () => {
const context = useContext(AmplitudeContext)
if (context === undefined) {
throw new Error('useAmplitude must be used within an AmplitudeProvider')
}
return context
}
import React, { createContext, useContext, useEffect } from 'react'
import * as amplitude from '@amplitude/analytics-browser'
import { autocapturePlugin } from '@amplitude/plugin-autocapture-browser'
interface AmplitudeContextType {
track: (eventName: string, eventProperties?: Record<string, any>) => void
identify: (userId: string) => void
}
const AmplitudeContext = createContext<AmplitudeContextType | undefined>(undefined)
export interface AmplitudeProviderProps {
children: React.ReactNode
apiKey: string
}
export const AmplitudeProvider: React.FC<AmplitudeProviderProps> = ({ children, apiKey }) => {
useEffect(() => {
if (!apiKey) return
const plugin = autocapturePlugin()
amplitude.add(plugin)
amplitude.init(apiKey, undefined, {
defaultTracking: {
sessions: true,
pageViews: true,
formInteractions: true,
fileDownloads: true,
},
fetchRemoteConfig: true,
autocapture: true,
serverZone: amplitude.Types.ServerZone.EU,
})
}, [apiKey])
const track = (eventName: string, eventProperties?: Record<string, any>) => {
if (apiKey) {
amplitude.track(eventName, eventProperties)
}
}
const identify = (userId: string) => {
amplitude.setUserId(userId)
}
return (
<AmplitudeContext.Provider value={{ track, identify }}>{children}</AmplitudeContext.Provider>
)
}
export const useAmplitude = () => {
const context = useContext(AmplitudeContext)
if (context === undefined) {
throw new Error('useAmplitude must be used within an AmplitudeProvider')
}
return context
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment