Skip to content
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

Make Analytics component stable #2141

Merged
merged 26 commits into from
May 28, 2024
Merged

Make Analytics component stable #2141

merged 26 commits into from
May 28, 2024

Conversation

wizardlyhel
Copy link
Contributor

@wizardlyhel wizardlyhel commented May 22, 2024

WHY are these changes introduced?

Making <Analytics> stable

WHAT is this pull request doing?

  • Make <UNSTABLE_Analytics> stable - rename to <Analytics>
  • Make unstable_useAnalytics stable - rename to useAnalytics
  • Update all docs and examples
  • Implement analytics component in skeleton template
  • Update examples with skeleton update
  • Fix useCustomerPrivacy so that it will be ready when script is loaded
  • Make sure <Analytics.Provider> works for mock shop
  • Add optional cookieDomain prop for <Analytics.Provider> so that useShopifyCookie can use the domain override
  • update analytics example to a Goggle Tag Manager example

HOW to test your changes?

Post-merge steps

Checklist

  • I've read the Contributing Guidelines
  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've added a changeset if this PR contains user-facing or noteworthy changes
  • I've added tests to cover my changes
  • I've added or updated the documentation
Copy link
Contributor

shopify bot commented May 22, 2024

Oxygen deployed a preview of your hl-stable-analytics branch. Details:

Storefront Status Preview link Deployment details Last update (UTC)
Skeleton (skeleton.hydrogen.shop) ✅ Successful (Logs) Preview deployment Inspect deployment May 28, 2024 8:02 PM
optimistic-cart-ui ✅ Successful (Logs) Preview deployment Inspect deployment May 28, 2024 8:02 PM
subscriptions ✅ Successful (Logs) Preview deployment Inspect deployment May 28, 2024 8:02 PM
custom-cart-method ✅ Successful (Logs) Preview deployment Inspect deployment May 28, 2024 8:02 PM
vite ✅ Successful (Logs) Preview deployment Inspect deployment May 28, 2024 8:02 PM
third-party-queries-caching ✅ Successful (Logs) Preview deployment Inspect deployment May 28, 2024 8:02 PM

Learn more about Hydrogen's GitHub integration.

This comment has been minimized.

@wizardlyhel wizardlyhel marked this pull request as draft May 22, 2024 21:01
@wizardlyhel
Copy link
Contributor Author

Remove the env var requirements from skeleton and will display the following errors when running npm run dev with mock shop

Screenshot 2024-05-23 at 10 08 23 AM
@wizardlyhel
Copy link
Contributor Author

wizardlyhel commented May 23, 2024

Updated the error to just a warning and prevent csp error from happening when using mock shop

Screenshot 2024-05-23 at 3 06 23 PM
@wizardlyhel wizardlyhel marked this pull request as ready for review May 24, 2024 17:23
Copy link
Contributor

@blittle blittle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the script hydration stuff, looks great! 🙌

.changeset/thin-hairs-travel.md Outdated Show resolved Hide resolved
examples/analytics/.env Outdated Show resolved Hide resolved
examples/gtm/README.md Outdated Show resolved Hide resolved
Comment on lines 152 to 158
+ <script nonce={nonce} dangerouslySetInnerHTML={{
+ __html: `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
+ new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
+ j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
+ 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
+ })(window,document,'script','dataLayer','GTM-<YOUR_GTM_ID>');`,
+ }}></script>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not use the Script component? As is, this will cause hydration warnings, because the nonce is unavailable in the client. suppressHydrationWarning needs to be added as a prop: https://github.com/Shopify/hydrogen/blob/main/packages/hydrogen/src/csp/Script.tsx#L10

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just trying to be as close to as the snippet that GTM supplied - but use <Script> to surpress hydration warning makes sense

@@ -129,18 +129,35 @@ export default function App() {
<meta name="viewport" content="width=device-width,initial-scale=1" />
<Meta />
<Links />
<script nonce={nonce} dangerouslySetInnerHTML={{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either add suppressHydrationWarning or use the Script component

Co-authored-by: Bret Little <bret.little@shopify.com>
wizardlyhel and others added 3 commits May 28, 2024 12:49
Co-authored-by: Bret Little <bret.little@shopify.com>
@wizardlyhel wizardlyhel merged commit 5d6465b into main May 28, 2024
13 checks passed
@wizardlyhel wizardlyhel deleted the hl-stable-analytics branch May 28, 2024 20:06
@scottdixon
Copy link
Contributor

Hey @wizardlyhel! Getting this weird flickering when checkoutDomain isn't defined (Skeleton template) https://screenshot.click/31-44-wuz9s-vi9vu.mp4

@napter
Copy link

napter commented Jun 10, 2024

@wizardlyhel - the gtm example on the main branch still says "unstable".
Also, the analytics example on the main site is 404'ing

@wizardlyhel
Copy link
Contributor Author

wizardlyhel commented Jun 10, 2024

@napter Ah, I missed the update on the heading text for the GTM readme but the example on the readme are all updated to the latest code.

We no longer have the analytics example in the Hydrogen repo. That is now baked into skeleton template and you may follow these docs https://shopify.dev/docs/storefronts/headless/hydrogen/analytics to update your app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants