Commit 882ff73
authored
Analytics improvements (tldraw#7041)
GTM Integration:
- New GTMAnalyticsService loads GTM script/iframe, tracks events via
dataLayer
- Consent banner tracking: `display_consent_banner` and
`select_consent_banner` events
- Public consent API: `getConsentState()` and `onConsentUpdate()` on
`window.tlanalytics`
- Structured event tracking: `trackCopyCode()` and
`trackFormSubmission()` methods
- Wired up code copy tracking in docs site
- Config: `TL_GTM_CONTAINER_ID` env var support
Dotcom Analytics:
- Added cross-domain tracking linker for tldraw.dev
- Push watermark click tracking to GTM
- Added watermark event to editor emit types
Things to do:
- [ ] Set NEXT_PUBLIC_GTM_CONTAINER_ID in Vercel
- [ ] Remove NEXT_PUBLIC_GA4_MEASUREMENT_ID variable in Vercel
- [ ] Make similar changes on the Framer side of things. Code change
below.
Add this to head:
```typescript
<script>
window.TL_GTM_CONTAINER_ID = 'GTM-PJDV58LL';
</script>
<script
id="tldraw-analytics"
type="text/javascript"
async
defer
src="https://analytics.tldraw.com/tl-analytics.js"
></script>
<script>
// Send form submission events to GTM
document.addEventListener("framer:formsubmit", async (event) => {
if (window.tlanalytics?.trackFormSubmission) {
const formData = event.detail.data || {};
// Build payload with only fields that have values
const payload = {
enquiry_type: event.detail.trackingId || 'form_submission',
};
if (formData.email) {
payload.user_email = formData.email;
payload.user_email_sha256 = await sha256(formData.email.toLowerCase());
}
if (formData.first_name) {
payload.user_first_name = formData.first_name;
}
if (formData.last_name) {
payload.user_last_name = formData.last_name;
}
if (formData.linkedin_company_page) {
payload.company_linkedin = formData.linkedin_company_page;
}
if (formData.hs_employee_range) {
payload.company_size = formData.hs_employee_range;
}
if (formData.website) {
payload.company_website = formData.website;
}
if (formData.country) {
payload.user_country = formData.country;
}
// Only call if we have required fields
if (payload.user_email) {
window.tlanalytics.trackFormSubmission(payload);
}
}
});
// Track code copy events
document.addEventListener('copy', (copyEvent) => {
const isWithinCodeBlock = copyEvent.target?.closest('pre, code');
if (isWithinCodeBlock) {
const copiedText = window.getSelection()?.toString() || '';
// Only track if we have actual text
if (copiedText && window.tlanalytics?.trackCopyCode) {
window.tlanalytics.trackCopyCode({
page_category: 'framer',
text_snippet: copiedText,
});
}
}
});
// Simple SHA-256 hash function for email
async function sha256(message) {
const msgBuffer = new TextEncoder().encode(message);
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}
</script>
```
Add this to body:
```typescript
<noscript>
<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-PJDV58LL"
height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
```
### API changes
- Add click `click-watermark` emit type, so that you can listen to it
via the editor events.
### Change type
- [x] `improvement`
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> Integrates GTM with consent handling and new tracking APIs, wires
code-copy/form events, adds watermark click tracking end-to-end, and
updates allowed origins and docs embed.
>
> - **Analytics Core**:
> - **GTM Integration**: New `GTMAnalyticsService` loads GTM, manages
consent via `dataLayer`, and tracks `page_view`/custom events.
> - **Consent**: Adds `getConsentState()`/`onConsentUpdate()` and tracks
consent banner display/selection with opt-in type.
> - **Structured Events**: Adds `trackCopyCode()` and
`trackFormSubmission()` and forwards to services.
> - **Service Wiring**: Includes `gtmService` alongside existing
services.
> - **Docs Site (`apps/docs`)**:
> - Sets `window.TL_GTM_CONTAINER_ID`; loads analytics from local in
dev; adds GTM `<noscript>` iframe.
> - Wires code-copy to `tlanalytics.trackCopyCode`.
> - **Dotcom Analytics**:
> - GA4 linker for cross-domain tracking with `tldraw.dev`.
> - Tracks `click-watermark` in GA4.
> - **Editor SDK**:
> - Adds `click-watermark` event to `TLEventMap`/UI events; watermark
now emits it; `Tldraw` forwards to UI event system.
> - **Worker**:
> - Allows CORS for `http://localhost:3001`.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
853c2d3. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent c344a5a commit 882ff73
16 files changed
Lines changed: 471 additions & 13 deletions
File tree
- apps
- analytics-worker/src
- analytics/src
- analytics-services
- docs
- app
- components/content
- dotcom/client/src/utils
- packages
- editor
- src/lib
- editor/types
- license
- tldraw
- src/lib
- ui/context
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
Lines changed: 29 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
21 | 50 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
0 commit comments