Skip to content

feat: added ability to change tagmanager and analytics script src #408

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/content/scripts/analytics/google-analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ export const GoogleAnalyticsOptions = object({
* The Google Analytics ID.
*/
id: string(),
/**
* The script src.
* @default 'https://www.googletagmanager.com/gtag/js'
*/
src: optional(string()),
/**
* The datalayer's name you want it to be associated with
*/
Expand Down
5 changes: 5 additions & 0 deletions docs/content/scripts/tracking/google-tag-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ export const GoogleTagManagerOptions = object({
* The Google Tag Manager ID.
*/
id: string(),
/**
* The script src.
* @default 'https://www.googletagmanager.com/gtm.js'
*/
src: optional(string()),
/**
* The name of the dataLayer you want to use
* @default 'dataLayer'
Expand Down
4 changes: 2 additions & 2 deletions src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export const registry: (resolve?: (s: string) => string) => RegistryScripts = (r
},
logo: `<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 256 256"><path fill="#8AB4F8" d="m150.262 245.516l-44.437-43.331l95.433-97.454l46.007 45.091z"/><path fill="#4285F4" d="M150.45 53.938L106.176 8.731L9.36 104.629c-12.48 12.48-12.48 32.713 0 45.207l95.36 95.986l45.09-42.182l-72.654-76.407z"/><path fill="#8AB4F8" d="m246.625 105.37l-96-96c-12.494-12.494-32.756-12.494-45.25 0c-12.495 12.495-12.495 32.757 0 45.252l96 96c12.494 12.494 32.756 12.494 45.25 0c12.495-12.495 12.495-32.757 0-45.251"/><circle cx="127.265" cy="224.731" r="31.273" fill="#246FDB"/></svg>`,
scriptBundling(options) {
return withQuery('https://www.googletagmanager.com/gtag/js', { id: options?.id, l: options?.l })
return withQuery(options?.src || 'https://www.googletagmanager.com/gtm.js', { id: options?.id, l: options?.l })
},
},
{
Expand All @@ -259,7 +259,7 @@ export const registry: (resolve?: (s: string) => string) => RegistryScripts = (r
},
logo: `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" width="192px" height="192px" viewBox="0 0 192 192" enable-background="new 0 0 192 192" xml:space="preserve"><rect x="0" y="0" fill="none" width="192" height="192"/><g><g><path fill="#F9AB00" d="M130,29v132c0,14.77,10.19,23,21,23c10,0,21-7,21-23V30c0-13.54-10-22-21-22S130,17.33,130,29z"/></g><g><path fill="#E37400" d="M75,96v65c0,14.77,10.19,23,21,23c10,0,21-7,21-23V97c0-13.54-10-22-21-22S75,84.33,75,96z"/></g><g><circle fill="#E37400" cx="41" cy="163" r="21"/></g></g></svg>`,
scriptBundling(options) {
return withQuery('https://www.googletagmanager.com/gtag/js', { id: options?.id, l: options?.l })
return withQuery(options?.src || 'https://www.googletagmanager.com/gtag/js', { id: options?.id, l: options?.l })
},
},
{
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/registry/google-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type DataLayer = Array<Parameters<GTag> | Record<string, unknown>>
export const GoogleAnalyticsOptions = object({
id: string(),
l: optional(string()),
src: optional(string()),
})

export type GoogleAnalyticsInput = RegistryScriptInput<typeof GoogleAnalyticsOptions>
Expand All @@ -29,7 +30,7 @@ export interface GoogleAnalyticsApi {
export function useScriptGoogleAnalytics<T extends GoogleAnalyticsApi>(_options?: GoogleAnalyticsInput) {
return useRegistryScript<T, typeof GoogleAnalyticsOptions>(_options?.key || 'googleAnalytics', options => ({
scriptInput: {
src: withQuery('https://www.googletagmanager.com/gtag/js', { id: options?.id, l: options?.l }),
src: withQuery(options?.src || 'https://www.googletagmanager.com/gtag/js', { id: options?.id, l: options?.l }),
},
schema: import.meta.dev ? GoogleAnalyticsOptions : undefined,
scriptOptions: {
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/registry/google-tag-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ declare global {
export const GoogleTagManagerOptions = object({
id: string(),
l: optional(string()),
src: optional(string()),
})

export type GoogleTagManagerInput = RegistryScriptInput<typeof GoogleTagManagerOptions>

export function useScriptGoogleTagManager<T extends GoogleTagManagerApi>(_options?: GoogleTagManagerInput & { onBeforeGtmStart?: (gtag: GTag) => void }) {
return useRegistryScript<T, typeof GoogleTagManagerOptions>(_options?.key || 'googleTagManager', options => ({
scriptInput: {
src: withQuery('https://www.googletagmanager.com/gtm.js', { id: options?.id, l: options?.l }),
src: withQuery(options?.src || 'https://www.googletagmanager.com/gtm.js', { id: options?.id, l: options?.l }),
},
schema: import.meta.dev ? GoogleTagManagerOptions : undefined,
scriptOptions: {
Expand Down