-
Notifications
You must be signed in to change notification settings - Fork 326
fix perf tracking and make it optional from developer's end #1096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5ef05da
f4fc865
d325494
fa00f4a
482420f
5bd3da1
b497834
1470e4e
2d63174
7ffb462
d6d3182
1c231bc
eb60a17
33d42e5
d764f87
f717f87
49d51e5
afc50c9
1454119
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| 'create-hydrogen-app': minor | ||
| '@shopify/hydrogen': minor | ||
| --- | ||
|
|
||
| Make performance data available with ClientAnalytics and optional for developers to include |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import {useEffect} from 'react'; | ||
| import {loadScript} from '../../../../utilities'; | ||
| import {ClientAnalytics} from '../../index'; | ||
| import {useShop} from '../../../useShop'; | ||
|
|
||
| declare global { | ||
| interface Window { | ||
| BOOMR: any; | ||
| BOOMR_onload: any; | ||
| } | ||
| } | ||
|
|
||
| const URL = | ||
| 'https://cdn.shopify.com/shopifycloud/boomerang/shopify-boomerang-hydrogen.min.js'; | ||
|
|
||
| export function PerformanceMetrics() { | ||
| const {storeDomain} = useShop(); | ||
|
|
||
| useEffect(() => { | ||
| try { | ||
| (function () { | ||
| if ( | ||
| window.BOOMR && | ||
| (window.BOOMR.version || window.BOOMR.snippetExecuted) | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| // Executes only on first mount | ||
| window.BOOMR = window.BOOMR || {}; | ||
| window.BOOMR.hydrogenPerformanceEvent = (data: any) => { | ||
| ClientAnalytics.publish( | ||
| ClientAnalytics.eventNames.PERFORMANCE, | ||
| true, | ||
| data | ||
| ); | ||
| ClientAnalytics.pushToServer( | ||
| { | ||
| body: JSON.stringify(data), | ||
| }, | ||
| ClientAnalytics.eventNames.PERFORMANCE | ||
| ); | ||
| }; | ||
| window.BOOMR.storeDomain = storeDomain; | ||
|
|
||
| function boomerangSaveLoadTime(e: Event) { | ||
| window.BOOMR_onload = (e && e.timeStamp) || Date.now(); | ||
| } | ||
|
|
||
| // @ts-ignore | ||
| function boomerangInit(e) { | ||
| e.detail.BOOMR.init(); | ||
| e.detail.BOOMR.t_end = Date.now(); | ||
| } | ||
|
|
||
| if (window.addEventListener) { | ||
| window.addEventListener('load', boomerangSaveLoadTime, false); | ||
| // @ts-ignore | ||
| } else if (window.attachEvent) { | ||
| // @ts-ignore | ||
| window.attachEvent('onload', boomerangSaveLoadTime); | ||
| } | ||
| if (document.addEventListener) { | ||
| document.addEventListener('onBoomerangLoaded', boomerangInit); | ||
| // @ts-ignore | ||
| } else if (document.attachEvent) { | ||
| // @ts-ignore | ||
| document.attachEvent('onpropertychange', function (e) { | ||
| if (!e) e = event; | ||
| if (e.propertyName === 'onBoomerangLoaded') boomerangInit(e); | ||
| }); | ||
| } | ||
| })(); | ||
| loadScript(URL).catch(() => { | ||
| // ignore if boomerang doesn't load | ||
| // most likely because of an ad blocker | ||
| }); | ||
| } catch (err) { | ||
| // Do nothing | ||
| } | ||
| }, [storeDomain]); | ||
|
|
||
| return null; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| export function request( | ||
| request: Request, | ||
| data?: any, | ||
| contentType?: string | ||
| ): void { | ||
| const url = new URL(request.url); | ||
| if (url.search === '?performance' && contentType === 'json') { | ||
| const initTime = new Date().getTime(); | ||
|
|
||
| fetch('https://monorail-edge.shopifysvc.com/v1/produce', { | ||
| method: 'post', | ||
| headers: { | ||
| 'content-type': 'text/plain', | ||
| 'x-forwarded-for': request.headers.get('x-forwarded-for') || '', | ||
| 'user-agent': request.headers.get('user-agent') || '', | ||
| }, | ||
| body: JSON.stringify({ | ||
| schema_id: 'hydrogen_buyer_performance/2.0', | ||
| payload: data, | ||
| metadata: { | ||
| event_created_at_ms: initTime, | ||
| event_sent_at_ms: new Date().getTime(), | ||
| }, | ||
| }), | ||
| }).catch((error) => { | ||
| // send to bugsnag? oxygen? | ||
| }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import {useEffect} from 'react'; | ||
| import {ClientAnalytics} from '../../index'; | ||
|
|
||
| const PAD = 10; | ||
| let isInit = false; | ||
| export function PerformanceMetricsDebug() { | ||
| useEffect(() => { | ||
| if (!isInit) { | ||
| isInit = true; | ||
| ClientAnalytics.subscribe( | ||
| ClientAnalytics.eventNames.PERFORMANCE, | ||
| (data: any) => { | ||
| console.group(`Performance - ${data.page_load_type} load`); | ||
wizardlyhel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| logMetricIf('TTFB:', data.response_start - data.navigation_start); | ||
| logMetricIf('FCP:', data.first_contentful_paint); | ||
| logMetricIf('LCP:', data.largest_contentful_paint); | ||
| logMetricIf('Duration:', data.response_end - data.navigation_start); | ||
| console.groupEnd(); | ||
| } | ||
| ); | ||
| } | ||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could maybe add an empty dep array
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't - it still double executes. It's either this or that
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's due to StrictMode, correct? If so, it won't double execute in a real build; only in dev mode.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't want this to double execute, even in dev. Double firing of analytics should never happen no matter where you are. |
||
|
|
||
| return null; | ||
| } | ||
|
|
||
| function logMetricIf(lable: string, data: any | undefined) { | ||
| data && console.log(`${lable.padEnd(PAD)}${Math.round(data)} ms`); | ||
| } | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.