Skip to content

Commit

Permalink
feat: prevent ad blockers from affecting development (closes #85)
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Jul 25, 2024
1 parent 1537068 commit 1bcd2e8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
3 changes: 1 addition & 2 deletions src/runtime/composables/useGtag.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { withQuery } from 'ufo'
import { gtag, initGtag } from '../gtag'
import { gtag, initGtag, resolveTags } from '../utils'
import { disableAnalytics as _disableAnalytics, enableAnalytics as _enableAnalytics } from '../analytics'
import type { ModuleOptions } from '../../module'
import type { Gtag } from '../types'
import { resolveTags } from '../utils'
import { useHead, useRuntimeConfig } from '#imports'

export function useGtag() {
Expand Down
25 changes: 0 additions & 25 deletions src/runtime/gtag.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/runtime/plugin.client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { withQuery } from 'ufo'
import { defineNuxtPlugin } from 'nuxt/app'
import type { ModuleOptions } from '../module'
import { initGtag } from './gtag'
import { resolveTags } from './utils'
import { initGtag, resolveTags } from './utils'
import { useHead, useRuntimeConfig } from '#imports'

export default defineNuxtPlugin({
Expand Down
24 changes: 24 additions & 0 deletions src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@ import type { ModuleOptions } from '../module'
import type { GoogleTagOptions } from './types'
import { toRaw } from '#imports'

// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars
export function gtag(...args: any[]) {
// eslint-disable-next-line prefer-rest-params
window.dataLayer?.push(arguments)
}

/**
* Initialize the Google tag.
*/
export function initGtag({ tags }: { tags: GoogleTagOptions[] }) {
window.dataLayer = window.dataLayer || []

for (const tag of tags) {
for (const command of tag.initCommands ?? [])
gtag(...command)
}

gtag('js', new Date())
for (const tag of tags) {
// Always provide a default value for the `config` object
gtag('config', tag.id, tag.config ?? {})
}
}

export function resolveTags(options: Required<ModuleOptions>) {
const _options = toRaw(options)

Expand Down

0 comments on commit 1bcd2e8

Please sign in to comment.