@hanzo/ui 0.5.16 → 0.5.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export const FB_PIXEL_ID = process.env.NEXT_PUBLIC_FACEBOOK_PIXEL_ID
|
|
2
|
+
|
|
3
|
+
export const pageview = () => {
|
|
4
|
+
window.fbq('track', 'PageView')
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// https://developers.facebook.com/docs/facebook-pixel/advanced/
|
|
8
|
+
export const event = (name: string, options = {}) => {
|
|
9
|
+
window.fbq('track', name, options)
|
|
10
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useEffect, useState } from 'react'
|
|
4
|
+
import * as gtag from './gtag'
|
|
5
|
+
import Script from 'next/script'
|
|
6
|
+
import { usePathname } from 'next/navigation'
|
|
7
|
+
|
|
8
|
+
const GoogleAnalyticsHead = () => {
|
|
9
|
+
return (
|
|
10
|
+
<script
|
|
11
|
+
dangerouslySetInnerHTML={{
|
|
12
|
+
__html: `
|
|
13
|
+
window.dataLayer = window.dataLayer || [];
|
|
14
|
+
function gtag(){dataLayer.push(arguments);}
|
|
15
|
+
gtag('js', new Date());
|
|
16
|
+
|
|
17
|
+
gtag('config', '${gtag.GA_TRACKING_ID}', {
|
|
18
|
+
page_path: window.location.pathname,
|
|
19
|
+
});
|
|
20
|
+
`,
|
|
21
|
+
}}
|
|
22
|
+
/>
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const GoogleAnalytics = () => {
|
|
27
|
+
const [loaded, setLoaded] = useState(false)
|
|
28
|
+
const pathname = usePathname()
|
|
29
|
+
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (!loaded) return
|
|
32
|
+
|
|
33
|
+
gtag.pageview(pathname)
|
|
34
|
+
}, [pathname, loaded])
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<div>
|
|
38
|
+
<Script
|
|
39
|
+
strategy='afterInteractive'
|
|
40
|
+
onLoad={() => setLoaded(true)}
|
|
41
|
+
src={`https://www.googletagmanager.com/gtag/js?id=${gtag.GA_TRACKING_ID}`}
|
|
42
|
+
/>
|
|
43
|
+
</div>
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export {GoogleAnalyticsHead, GoogleAnalytics}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const GA_TRACKING_ID = process.env.NEXT_PUBLIC_GA_ID
|
|
2
|
+
|
|
3
|
+
// https://developers.google.com/analytics/devguides/collection/gtagjs/pages
|
|
4
|
+
export const pageview = (url: string) => {
|
|
5
|
+
window.gtag('config', GA_TRACKING_ID, {
|
|
6
|
+
page_path: url,
|
|
7
|
+
})
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// https://developers.google.com/analytics/devguides/collection/gtagjs/events
|
|
11
|
+
export const event = ({ action, category, label, value }: {action: string, category: string, label: string, value: string}) => {
|
|
12
|
+
window.gtag('event', action, {
|
|
13
|
+
event_category: category,
|
|
14
|
+
event_label: label,
|
|
15
|
+
value: value,
|
|
16
|
+
})
|
|
17
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { usePathname } from 'next/navigation'
|
|
4
|
+
import Script from 'next/script'
|
|
5
|
+
import { useEffect, useState } from 'react'
|
|
6
|
+
import * as fbq from './fpixel'
|
|
7
|
+
|
|
8
|
+
const FacebookPixelHead = () => {
|
|
9
|
+
return (
|
|
10
|
+
<noscript>
|
|
11
|
+
<img
|
|
12
|
+
height='1'
|
|
13
|
+
width='1'
|
|
14
|
+
style={{ display: 'none' }}
|
|
15
|
+
src={`https://www.facebook.com/tr?id=${fbq.FB_PIXEL_ID}&ev=PageView&noscript=1`}
|
|
16
|
+
/>
|
|
17
|
+
</noscript>
|
|
18
|
+
)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const FacebookPixel = () => {
|
|
22
|
+
const [loaded, setLoaded] = useState(false)
|
|
23
|
+
const pathname = usePathname()
|
|
24
|
+
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
if (!loaded) return
|
|
27
|
+
|
|
28
|
+
fbq.pageview()
|
|
29
|
+
}, [pathname, loaded])
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<div>
|
|
33
|
+
<Script
|
|
34
|
+
id='fb-pixel'
|
|
35
|
+
strategy='afterInteractive'
|
|
36
|
+
onLoad={() => setLoaded(true)}
|
|
37
|
+
dangerouslySetInnerHTML={{
|
|
38
|
+
__html: `
|
|
39
|
+
!function(f,b,e,v,n,t,s)
|
|
40
|
+
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
|
|
41
|
+
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
|
|
42
|
+
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
|
|
43
|
+
n.queue=[];t=b.createElement(e);t.async=!0;
|
|
44
|
+
t.src=v;s=b.getElementsByTagName(e)[0];
|
|
45
|
+
s.parentNode.insertBefore(t,s)}(window, document,'script',
|
|
46
|
+
'https://connect.facebook.net/en_US/fbevents.js');
|
|
47
|
+
fbq('init', ${fbq.FB_PIXEL_ID});
|
|
48
|
+
`,
|
|
49
|
+
}}
|
|
50
|
+
/>
|
|
51
|
+
</div>
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export {FacebookPixelHead, FacebookPixel}
|
package/next/root-layout.tsx
CHANGED
|
@@ -3,6 +3,8 @@ import React, { type PropsWithChildren } from 'react'
|
|
|
3
3
|
import Header from '../common/header'
|
|
4
4
|
import type { SiteDef } from '../types'
|
|
5
5
|
import getAppRouterBodyFontClasses from './get-app-router-font-classes'
|
|
6
|
+
import { FacebookPixelHead, FacebookPixel } from './analytics/pixel-analytics'
|
|
7
|
+
import { GoogleAnalytics, GoogleAnalyticsHead } from './analytics/google-analytics'
|
|
6
8
|
|
|
7
9
|
// Next 14: https://nextjs.org/docs/app/building-your-application/upgrading/codemods#use-viewport-export
|
|
8
10
|
const viewport = {
|
|
@@ -39,8 +41,12 @@ const RootLayout: React.FC<PropsWithChildren & {
|
|
|
39
41
|
<head >
|
|
40
42
|
{/* https://stackoverflow.com/a/75716588/11645689 */ }
|
|
41
43
|
<base target='_blank' />
|
|
44
|
+
<FacebookPixelHead/>
|
|
45
|
+
<GoogleAnalyticsHead/>
|
|
42
46
|
</head>
|
|
43
47
|
<body className={bodyClasses}>
|
|
48
|
+
<FacebookPixel />
|
|
49
|
+
<GoogleAnalytics />
|
|
44
50
|
{header && <Header siteDef={siteDef}/>}
|
|
45
51
|
{children}
|
|
46
52
|
</body>
|
package/package.json
CHANGED