-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
206 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React from 'react' | ||
import { AnalyticsBrowser } from '../../' | ||
|
||
const AnalyticsContext = React.createContext<AnalyticsBrowser>(undefined) | ||
|
||
export const AnalyticsProvider: React.FC<{ writeKey: string }> = ({ | ||
children, | ||
writeKey, | ||
}) => { | ||
const analytics = React.useMemo( | ||
() => AnalyticsBrowser.load({ writeKey }), | ||
[writeKey] | ||
) | ||
return ( | ||
<AnalyticsContext.Provider value={analytics}> | ||
{children} | ||
</AnalyticsContext.Provider> | ||
) | ||
} | ||
|
||
export const useAnalytics = () => React.useContext(AnalyticsContext) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import Link from 'next/link' | ||
import { AnalyticsBrowser } from '../../../' | ||
|
||
export const analytics = AnalyticsBrowser.load({ | ||
writeKey: process.env.NEXT_PUBLIC_WRITEKEY, | ||
}) | ||
|
||
analytics.then(() => console.log('loaded!')) | ||
|
||
const Vanilla: React.FC = () => { | ||
analytics.identify('hello').then((res) => console.log('identified!', res)) | ||
return ( | ||
<div> | ||
<input | ||
style={{ | ||
display: 'block', | ||
margin: '1rem 0', | ||
}} | ||
value="submit" | ||
onClick={(e) => { | ||
e.preventDefault() | ||
analytics | ||
.track('vanilla click') | ||
.then((res) => console.log('tracked!', res)) | ||
}} | ||
type="submit" | ||
/> | ||
<Link href={'/vanilla/other-page'}>Go to Other Page</Link> | ||
</div> | ||
) | ||
} | ||
|
||
export default Vanilla |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import Link from 'next/link' | ||
import React, { useEffect } from 'react' | ||
import { analytics } from '.' | ||
|
||
const OtherPage: React.FC = () => { | ||
useEffect(() => { | ||
analytics.identify('hello').then((res) => console.log('identified!', res)) | ||
}, []) | ||
return ( | ||
<div> | ||
<input | ||
style={{ | ||
display: 'block', | ||
margin: '1rem 0', | ||
}} | ||
value="Track!" | ||
onClick={(e) => { | ||
e.preventDefault() | ||
analytics | ||
.track('clicked other page!') | ||
.then((res) => console.log('tracked!', res)) | ||
}} | ||
type="submit" | ||
/> | ||
<Link href={'/vanilla'}>Vanilla Home Link</Link> | ||
</div> | ||
) | ||
} | ||
|
||
export default OtherPage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { AnalyticsProvider, useAnalytics } from '../../context/analytics' | ||
import { useWriteKey } from '../../utils/hooks/useWritekey' | ||
|
||
const App = () => { | ||
const analytics = useAnalytics() | ||
return ( | ||
<button onClick={() => analytics.track('hello world').then(console.log)}> | ||
Track! | ||
</button> | ||
) | ||
} | ||
|
||
export default () => { | ||
const [writeKey] = useWriteKey() | ||
return ( | ||
<AnalyticsProvider writeKey={writeKey}> | ||
<App /> | ||
</AnalyticsProvider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ body { | |
|
||
a { | ||
color: inherit; | ||
text-decoration: none; | ||
text-decoration: underline; | ||
} | ||
|
||
* { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { useLocalStorage } from "./useLocalStorage"; | ||
|
||
export const useWriteKey = () => useLocalStorage( | ||
'segment_playground_write_key', | ||
process.env.NEXT_PUBLIC_WRITEKEY | ||
) |