-
Notifications
You must be signed in to change notification settings - Fork 87
feat: Ahrefs Web Analytics #751
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
adb0118
feat: Ahrefs Web Analytics
harlan-zw 2c16aec
test(ahrefs): gate beacon assertions on AHREFS_TEST_KEY
harlan-zw e883a0d
test(ahrefs): stub /api/event for deterministic beacon assertions
harlan-zw 07a4212
fix(ahrefs): route bundled beacons through the proxy
harlan-zw 8e4b9fb
docs(ahrefs): align ahrefs-analytics page with established pattern
harlan-zw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,70 @@ | ||
| --- | ||
| title: Ahrefs Web Analytics | ||
| description: Use Ahrefs Web Analytics in your Nuxt app to track page views and custom events with a privacy-first, cookie-less analytics script. | ||
| links: | ||
| - label: Source | ||
| icon: i-simple-icons-github | ||
| to: https://github.com/nuxt/scripts/blob/main/packages/script/src/runtime/registry/ahrefs-analytics.ts | ||
| size: xs | ||
| --- | ||
|
|
||
| [Ahrefs Web Analytics](https://ahrefs.com/web-analytics) is a privacy-first, cookie-less web analytics service from [Ahrefs](https://ahrefs.com) that tracks page views and custom events without sharing visitor data with third parties. | ||
|
|
||
| ::script-stats | ||
| :: | ||
|
|
||
| ::script-docs | ||
| :: | ||
|
|
||
| The composable comes with the following defaults: | ||
| - **Trigger: Client** Script will load when Nuxt is hydrating. | ||
|
|
||
| You can access the `AhrefsAnalytics` object as a proxy directly or await the `$script` promise to access the object. It's recommended to use the proxy for any void functions. | ||
|
|
||
| ::code-group | ||
|
|
||
| ```ts [Proxy] | ||
| const { proxy } = useScriptAhrefsAnalytics({ | ||
| key: 'your-project-key', | ||
| }) | ||
| function trackSignup() { | ||
| proxy.AhrefsAnalytics.sendEvent('signup', { | ||
| props: { plan: 'pro' }, | ||
| }) | ||
| } | ||
| ``` | ||
|
|
||
| ```ts [onLoaded] | ||
| const { onLoaded } = useScriptAhrefsAnalytics({ | ||
| key: 'your-project-key', | ||
| }) | ||
| onLoaded(({ AhrefsAnalytics }) => { | ||
| AhrefsAnalytics.sendEvent('signup', { | ||
| props: { plan: 'pro' }, | ||
| }) | ||
| }) | ||
| ``` | ||
|
|
||
| :: | ||
|
|
||
| ## SPA navigation | ||
|
|
||
| Ahrefs Analytics tracks single-page-app navigations natively: the loaded `analytics.js` patches `history.pushState` and listens for `popstate`, firing a fresh page-view whenever the URL changes. No extra configuration is needed for Nuxt route changes. | ||
|
Check warning on line 52 in docs/content/scripts/ahrefs-analytics.md
|
||
|
|
||
| ::script-types | ||
| :: | ||
|
|
||
| ## Example | ||
|
|
||
| Loading Ahrefs Web Analytics through `app.vue` when Nuxt is ready. | ||
|
|
||
| ```vue [app.vue] | ||
| <script setup lang="ts"> | ||
| useScriptAhrefsAnalytics({ | ||
| key: 'your-project-key', | ||
| scriptOptions: { | ||
| trigger: 'onNuxtReady' | ||
| } | ||
| }) | ||
| </script> | ||
| ``` | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,60 @@ | ||
| import type { RegistryScriptInput, UseScriptContext } from '#nuxt-scripts/types' | ||
| import { useRegistryScript } from '../utils' | ||
| import { AhrefsAnalyticsOptions } from './schemas' | ||
|
|
||
| export { AhrefsAnalyticsOptions } | ||
|
|
||
| export type AhrefsAnalyticsInput = RegistryScriptInput<typeof AhrefsAnalyticsOptions, true, false> | ||
|
|
||
| export interface AhrefsAnalyticsSendEventOptions { | ||
| /** Custom dimensions sent under `props`. */ | ||
| props?: Record<string, string> | ||
| /** Arbitrary metadata sent under `meta`. */ | ||
| meta?: Record<string, unknown> | ||
| /** Optional callback invoked once the beacon request completes. */ | ||
| callback?: (result?: { status?: number }) => void | ||
| } | ||
|
|
||
| export interface AhrefsAnalyticsInstance { | ||
| /** | ||
| * Manually send an event to Ahrefs Analytics. The script auto-fires | ||
| * page-view events on initial load and on `history.pushState`/`popstate`, | ||
| * so SPA navigations are tracked without calling this. | ||
| */ | ||
| sendEvent: (name: string, options?: AhrefsAnalyticsSendEventOptions) => void | ||
| } | ||
|
|
||
| export interface AhrefsAnalyticsApi { | ||
| AhrefsAnalytics: AhrefsAnalyticsInstance | ||
| } | ||
|
|
||
| declare global { | ||
| interface Window extends AhrefsAnalyticsApi {} | ||
| } | ||
|
|
||
| /** | ||
| * Load Ahrefs Web Analytics and expose its `sendEvent` API. | ||
| * | ||
| * The script attaches `window.AhrefsAnalytics` once loaded, fires an initial | ||
| * page-view, and tracks SPA navigations natively by patching | ||
| * `history.pushState` and listening to `popstate`. | ||
| * | ||
| * @see https://ahrefs.com/web-analytics | ||
| */ | ||
| export function useScriptAhrefsAnalytics<T extends AhrefsAnalyticsApi>( | ||
| _options?: AhrefsAnalyticsInput, | ||
| ): UseScriptContext<T> { | ||
| return useRegistryScript<T, typeof AhrefsAnalyticsOptions>('ahrefsAnalytics', options => ({ | ||
| scriptInput: { | ||
| 'src': 'https://analytics.ahrefs.com/analytics.js', | ||
| 'data-key': options.key, | ||
| 'crossorigin': false, | ||
| }, | ||
| schema: import.meta.dev ? AhrefsAnalyticsOptions : undefined, | ||
| scriptOptions: { | ||
| use() { | ||
| return { AhrefsAnalytics: window.AhrefsAnalytics } | ||
| }, | ||
| }, | ||
| }), _options) | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Google AdSenseis listed under the wrong privacy tier.The "IP only" row here includes
Google AdSense, but according toFIRST_PARTY.md, AdSense shares Google Analytics's proxy config and usesPRIVACY_HEATMAP(IP + language + hardware). It should either be removed from this row or moved to Heatmap-safe alongside Google Analytics.π Suggested fix
And update the Heatmap-safe row:
π€ Prompt for AI Agents