Skip to content

Commit 4377512

Browse files
feat(analytics): add 51LA analytics integration
Add 51LAAnalytics.astro component with production-only script injection. Integrate analytics into BaseLayout.astro with environment-based LA ID configuration. Emit hagicode:analytics-ready event when SDK initializes. Co-Authored-By: Hagicode <noreply@hagicode.com> Signed-off-by: newbe36524 <newbe36524@qq.com>
1 parent 487c587 commit 4377512

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

src/components/51LAAnalytics.astro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
const default51LAId = "L6b88a5yK4h2Xnci"
3+
const laId = import.meta.env.VITE_51LA_ID || default51LAId
4+
const isProduction = import.meta.env.PROD
5+
---
6+
7+
{isProduction && (
8+
<>
9+
<script is:inline charset="UTF-8" id="LA_COLLECT" src="//sdk.51.la/js-sdk-pro.min.js"></script>
10+
<script is:inline define:vars={{ laId }}>
11+
if (typeof LA !== "undefined" && typeof LA.init === "function") {
12+
LA.init({ id: laId, ck: laId, autoTrack: true, hashMode: true, screenRecord: true });
13+
window.dispatchEvent(new Event("hagicode:analytics-ready"));
14+
}
15+
</script>
16+
</>
17+
)}

src/layouts/BaseLayout.astro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
import "@/index.css"
3+
import Analytics51LA from "@/components/51LAAnalytics.astro"
34
import { PromoteCard } from "@/components/promote/PromoteCard"
45
import promoteCardStyles from "@/components/promote/PromoteCard.module.css"
56
import type { PageMetadata } from "@/lib/metadata-builder"
@@ -56,5 +57,6 @@ const { metadata, lang = "en" } = Astro.props as Props
5657
<body>
5758
<slot />
5859
<PromoteCard locale={lang} className={promoteCardStyles.promoteCard} client:load />
60+
<Analytics51LA />
5961
</body>
6062
</html>

0 commit comments

Comments
 (0)