Skip to content

fix(ga4): inject gtag.js when script tag is missing#6

Merged
Innei merged 1 commit into
masterfrom
fix/ga4-gtag-script-loading
Apr 13, 2026
Merged

fix(ga4): inject gtag.js when script tag is missing#6
Innei merged 1 commit into
masterfrom
fix/ga4-gtag-script-loading

Conversation

@Innei
Copy link
Copy Markdown
Member

@Innei Innei commented Apr 13, 2026

The stub gtag function is always assigned before the load check, so the
previous typeof gtag === 'function' guard never skipped loading. Inject
gtag/js when no matching script exists, matching the standard GA snippet.

Made-with: Cursor

The stub gtag function is always assigned before the load check, so the
previous typeof gtag === 'function' guard never skipped loading. Inject
gtag/js when no matching script exists, matching the standard GA snippet.

Made-with: Cursor
Copilot AI review requested due to automatic review settings April 13, 2026 13:38
@lobehubbot
Copy link
Copy Markdown
Member

👍 @Innei


Thank you for raising your pull request and contributing to our Community
Please make sure you have followed our contributing guidelines. We will review it as soon as possible.
If you encounter any problems, please feel free to connect with us.
非常感谢您提出拉取请求并为我们的社区做出贡献,请确保您已经遵循了我们的贡献指南,我们会尽快审查它。
如果您遇到任何问题,请随时与我们联系。

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

We've reviewed this pull request using the Sourcery rules engine

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 13, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@lobehub/analytics@6

commit: 120a26a

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes GA4 initialization so gtag.js is injected when the script tag is missing (previous typeof gtag guard was ineffective because a stub gtag is always assigned).

Changes:

  • Remove the ineffective typeof (window as any).gtag === 'function' load guard.
  • Detect existing GA script tags via document.querySelector(...) and inject gtag/js only when missing.
  • Add clarifying comments explaining why the new approach matches the standard GA snippet behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/providers/ga4.ts
Comment on lines +49 to +57
const existingScript = document.querySelector(
`script[src*="gtag/js"], script[id*="ga"], script[id*="gtag"]`,
);

if (!existingScript) {
const script = document.createElement('script');
script.async = true;
script.src = `https://www.googletagmanager.com/gtag/js?id=${this.config.measurementId}`;
document.head.append(script);
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

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

initialize() guards typeof window === 'undefined', but this new path unconditionally uses document.querySelector/document.head.append. In environments where window exists but document is missing (or document.head is null), this will throw and prevent initialization. Add an explicit typeof document === 'undefined' (and/or !document.head) guard with a logged error + early return.

Copilot uses AI. Check for mistakes.
Comment thread src/providers/ga4.ts
// decide whether to load gtag.js — that branch was never taken. Inject the script when the
// tag is missing from the document (standard GA snippet queues until the script loads).
const existingScript = document.querySelector(
`script[src*="gtag/js"], script[id*="ga"], script[id*="gtag"]`,
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

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

The selector script[id*="ga"] is extremely broad (e.g., it matches an unrelated <script id="tag">), which can produce false positives and skip injecting gtag/js even when GA4 isn’t present. Consider narrowing detection to GA-specific markers (e.g., script[src*="googletagmanager.com/gtag/js"] and/or stricter ids like id^="gtag"/id^="ga4").

Suggested change
`script[src*="gtag/js"], script[id*="ga"], script[id*="gtag"]`,
`script[src*="googletagmanager.com/gtag/js"], script[id^="gtag"], script[id^="ga4"]`,

Copilot uses AI. Check for mistakes.
@Innei Innei merged commit 7609553 into master Apr 13, 2026
9 checks passed
@lobehubbot
Copy link
Copy Markdown
Member

❤️ Great PR @Innei ❤️


The growth of project is inseparable from user feedback and contribution, thanks for your contribution!
项目的成长离不开用户反馈和贡献,感谢您的贡献!

@lobehubbot
Copy link
Copy Markdown
Member

🎉 This PR is included in version 1.6.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants