1npm install @june-so/analytics-next --save
To install the client-side API, you can use the custom script generator in the Quick Start Guide or get the package from NPM.
1npm install @june-so/analytics-next --save
After installing the NPM app, import AnalyticsBrowser
. An example
configuration for a React app is as follows:
1import { AnalyticsBrowser } from '@june-so/analytics-next'; 2 3function App() { 4// Create a state container for the analytics instance. 5const [analytics, setAnalytics] = useState(undefined); 6 7// On component mount, create an AnalyticsBrowser instance. 8useEffect(() => { 9const loadAnalytics = async () => { 10let [response] = await AnalyticsBrowser.load({ 11writeKey: 'YOUR_WRITE_KEY', 12}); 13setAnalytics(response); 14}; 15loadAnalytics(); 16}, []); 17 18useEffect(() => { 19// Send a pageview event once Analytics is ready. 20if (analytics) analytics.page(); 21}, [analytics]); 22 23return <div>Hello world!</div>; 24}
Here's a convenience React hook that will make integrating June significantly simpler:
1import { useEffect } from 'react'; 2import { AnalyticsBrowser } from '@june-so/analytics-next'; 3 4export function useJune(writeKey) { 5const [analytics, setAnalytics] = useState(undefined); 6 7useEffect(() => { 8const loadAnalytics = async () => { 9let [response] = await AnalyticsBrowser.load({ 10writeKey, 11}); 12setAnalytics(response); 13}; 14loadAnalytics(); 15}, [writeKey]); 16 17return analytics; 18}
Using the hook, you can store the Analytics.js instance in a context that makes it easier to use across many components.
To begin, define a context:
1import { useJune } from './hooks'; 2 3const JUNE_KEY = 'YOUR_WRITE_KEY'; 4 5export const JuneContext = createContext(null); 6 7export function JuneProvider({ children }) { 8const analytics = useJune(JUNE_KEY); 9 10return <JuneContext.Provider value={analytics}>{children}</JuneContext.Provider>; 11}
Then, at the top level of your app, provide the June context:
1import { JuneProvider } from './june'; 2 3function App({ children }) { 4return <JuneProvider>{children}</JuneProvider>; 5}
Now you can use the context in any component under the provider, allowing you to track events throughout your app. An example:
1import { useContext, useCallback } from 'react'; 2import { JuneProvider, JuneContext } from './june'; 3 4function PurchaseButton() { 5const analytics = useContext(JuneContext); 6 7const trackPurchase = useCallback(() => { 8if (analytics) analytics.track('Purchase Event'); 9}, [analytics]); 10 11return <button onClick={trackPurchase}>Purchase coat</button>; 12}
For a NextJS app, the best approach is installing the NPM package rather than using the inlined script. Client-side tracking is rather similar to the React example cited above.
You may find yourself wanting to track server events. In that case, import AnalyticsNode
within the SSR portions of the app:
1import { AnalyticsNode } from '@june-so/analytics-next'; 2 3// Set the environment variable NEXT_PUBLIC_JUNE_KEY with your SDK write key. 4const JUNE_KEY = process.env.NEXT_PUBLIC_JUNE_KEY ?? 'api-key'; 5 6// SSR function that runs before page 7export async function getServerSideProps(context) { 8const [nodeAnalytics] = await AnalyticsNode.load({ 9// Initialize with your writeKey 10writeKey: JUNE_KEY, 11}); 12 13// Send a test event 14await nodeAnalytics.track('Test Event Node'); 15 16return { 17props: {}, 18}; 19}
Tracking pageview events requires special code to be placed in the App
component of your NextJS codebase.
1import { useEffect } from 'react'; 2import { useRouter } from 'next/router'; 3 4export default function MyApp({ Component, pageProps }) { 5const analytics = useJune('API-KEY'); 6const router = useRouter(); 7 8useEffect(() => { 9const handleRouteChange = (url, { shallow }) => { 10if (analytics) analytics.page(url); 11}; 12 13router.events.on('routeChangeStart', handleRouteChange); 14 15return () => { 16router.events.off('routeChangeStart', handleRouteChange); 17}; 18}, [analytics]); 19 20return <Component {...pageProps} />; 21}
Using the hook and environment variables, you can implement June quite nicely on a NextJS app.
1import { useCallback } from 'react'; 2import { useJune } from './hooks'; 3 4function Index() { 5const analytics = useJune(process.env.NEXT_PUBLIC_JUNE_KEY ?? 'api-key'); 6 7const trackPurchase = useCallback(() => { 8if (analytics) analytics.track('Purchase Event'); 9}, [analytics]); 10 11return <button onClick={trackPurchase}>Purchase coat</button>; 12}