-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Shopify Analitics Package to test it
- Loading branch information
1 parent
970c702
commit 7993c9b
Showing
4 changed files
with
99 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const SHOP_DATA = `#graphql | ||
query Shop { | ||
shop{ | ||
id | ||
name | ||
} | ||
}` |
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,40 @@ | ||
import { useMatches } from '@remix-run/react'; | ||
import { useMemo } from 'react'; | ||
|
||
const DEFAULT_LOCALE = {/* Empty for demonstration purposes.*/ } | ||
|
||
/** | ||
* Custom hook to aggregate `analytics` data returned by Remix loaders | ||
* @param hasUserConsent {Boolean} | ||
* @returns Object with aggregated analytics data | ||
*/ | ||
export function usePageAnalytics({ hasUserConsent }) { | ||
// useMatches returns an array of nested routes | ||
const matches = useMatches(); | ||
|
||
// useMemo improves performance by only running when `matches` changes. | ||
const analyticsFromMatches = useMemo(() => { | ||
const data = {}; | ||
|
||
// For each nested route, get the `analytics` object returned | ||
// by its loader function and assign to `data` | ||
matches.forEach((event) => { | ||
const eventData = event?.data; | ||
if (eventData) { | ||
eventData['analytics'] && Object.assign(data, eventData['analytics']); | ||
|
||
// This hook also allows you to optionally collect data such as | ||
// currency and language preferences. | ||
const selectedLocale = eventData['selectedLocale'] || DEFAULT_LOCALE; | ||
Object.assign(data, { | ||
currency: selectedLocale.currency, | ||
acceptedLanguage: selectedLocale.language, | ||
}); | ||
} | ||
}); | ||
|
||
return { ...data, hasUserConsent }; | ||
}, [matches]); | ||
|
||
return analyticsFromMatches; | ||
} |
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