-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add perfkit * add spa mode * working perfkit * format * format * fix test * Fix test * set up perfkit in test * address errors * guard perfkit * pinned perfkit version
- Loading branch information
1 parent
24f9c71
commit c094ecc
Showing
4 changed files
with
78 additions
and
3 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,60 @@ | ||
import {parseGid, useLoadScript} from '@shopify/hydrogen-react'; | ||
import {ShopAnalytics, useAnalytics} from './AnalyticsProvider'; | ||
import {AnalyticsEvent} from './events'; | ||
import {useEffect, useRef} from 'react'; | ||
|
||
declare global { | ||
interface Window { | ||
PerfKit: { | ||
navigate: () => void; | ||
setPageType: (pageType: string) => void; | ||
}; | ||
} | ||
} | ||
|
||
// Pin to a version that have SPA support. | ||
// TODO: Update to a stable version when available. | ||
const PERF_KIT_UNSTABLE = | ||
'https://cdn.shopify.com/shopifycloud/perf-kit/shopify-perf-kit-1bd852a.min.js'; | ||
|
||
export function PerfKit({shop}: {shop: ShopAnalytics}) { | ||
const loadedEvent = useRef(false); | ||
const {subscribe, register} = useAnalytics(); | ||
const {ready} = register('Internal_Shopify_Perf_Kit'); | ||
|
||
const scriptStatus = useLoadScript(PERF_KIT_UNSTABLE, { | ||
attributes: { | ||
id: 'perfkit', | ||
'data-application': 'hydrogen', | ||
'data-shop-id': parseGid(shop.shopId).id.toString(), | ||
'data-storefront-id': shop.hydrogenSubchannelId, | ||
'data-monorail-region': 'global', | ||
'data-spa-mode': 'true', | ||
'data-resource-timing-sampling-rate': '100', | ||
}, | ||
}); | ||
|
||
useEffect(() => { | ||
if (scriptStatus !== 'done' || loadedEvent.current) return; | ||
loadedEvent.current = true; | ||
|
||
subscribe(AnalyticsEvent.PAGE_VIEWED, () => { | ||
window.PerfKit?.navigate(); | ||
}); | ||
subscribe(AnalyticsEvent.PRODUCT_VIEWED, () => { | ||
window.PerfKit?.setPageType('product'); | ||
}); | ||
subscribe(AnalyticsEvent.COLLECTION_VIEWED, () => { | ||
window.PerfKit?.setPageType('collection'); | ||
}); | ||
subscribe(AnalyticsEvent.SEARCH_VIEWED, () => { | ||
window.PerfKit?.setPageType('search'); | ||
}); | ||
subscribe(AnalyticsEvent.CART_VIEWED, () => { | ||
window.PerfKit?.setPageType('cart'); | ||
}); | ||
|
||
ready(); | ||
}, [subscribe, ready, scriptStatus]); | ||
return null; | ||
} |
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