mirror of https://github.com/grafana/grafana
Echo: Add support for Google Analytics 4 (#55446)
* user essentials mob! 🔱 lastFile:public/app/core/services/echo/backends/analytics/GA4Backend.ts * user essentials mob! 🔱 * user essentials mob! 🔱 lastFile:public/app/core/services/echo/backends/analytics/GA4Backend.ts * user essentials mob! 🔱 lastFile:public/app/core/services/echo/backends/analytics/GA4Backend.ts * user essentials mob! 🔱 lastFile:public/app/app.ts * user essentials mob! 🔱 Co-authored-by: eledobleefe <laura.fernandez@grafana.com> Co-authored-by: Leodegario Pasakdal <leodegario.pasakdal@grafana.com>pull/55452/head
parent
13146cc812
commit
d014a3a09b
@ -0,0 +1,58 @@ |
|||||||
|
import $ from 'jquery'; |
||||||
|
|
||||||
|
import { CurrentUserDTO } from '@grafana/data'; |
||||||
|
import { EchoBackend, EchoEventType, PageviewEchoEvent } from '@grafana/runtime'; |
||||||
|
|
||||||
|
import { getUserIdentifier } from '../../utils'; |
||||||
|
|
||||||
|
declare global { |
||||||
|
interface Window { |
||||||
|
dataLayer: unknown[]; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export interface GA4EchoBackendOptions { |
||||||
|
googleAnalyticsId: string; |
||||||
|
user?: CurrentUserDTO; |
||||||
|
} |
||||||
|
|
||||||
|
export class GA4EchoBackend implements EchoBackend<PageviewEchoEvent, GA4EchoBackendOptions> { |
||||||
|
supportedEvents = [EchoEventType.Pageview]; |
||||||
|
trackedUserId: number | null = null; |
||||||
|
|
||||||
|
constructor(public options: GA4EchoBackendOptions) { |
||||||
|
const url = `https://www.googletagmanager.com/gtag/js?id=${options.googleAnalyticsId}`; |
||||||
|
|
||||||
|
$.ajax({ |
||||||
|
url, |
||||||
|
dataType: 'script', |
||||||
|
cache: true, |
||||||
|
}); |
||||||
|
|
||||||
|
window.dataLayer = window.dataLayer || []; |
||||||
|
window.gtag = function gtag() { |
||||||
|
window.dataLayer.push(arguments); |
||||||
|
}; |
||||||
|
window.gtag('js', new Date()); |
||||||
|
|
||||||
|
const configOptions: Gtag.CustomParams = {}; |
||||||
|
|
||||||
|
if (options.user) { |
||||||
|
configOptions.user_id = getUserIdentifier(options.user); |
||||||
|
} |
||||||
|
|
||||||
|
window.gtag('config', options.googleAnalyticsId, configOptions); |
||||||
|
} |
||||||
|
|
||||||
|
addEvent = (e: PageviewEchoEvent) => { |
||||||
|
if (!window.gtag) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
window.gtag('set', 'page_path', e.payload.page); |
||||||
|
window.gtag('event', 'page_view'); |
||||||
|
}; |
||||||
|
|
||||||
|
// Not using Echo buffering, addEvent above sends events to GA as soon as they appear
|
||||||
|
flush = () => {}; |
||||||
|
} |
Loading…
Reference in new issue