Skip to content

Commit

Permalink
ga4-params Move new code to TS file version
Browse files Browse the repository at this point in the history
  • Loading branch information
corvidism committed Apr 8, 2024
1 parent 5cd9955 commit c69c207
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 172 deletions.
168 changes: 0 additions & 168 deletions packages/plugin-analytic-google/src/GoogleAnalytics4.js

This file was deleted.

21 changes: 17 additions & 4 deletions packages/plugin-analytic-google/src/GoogleAnalytics4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type AnalyticGoogleSettings = {
*/
export class GoogleAnalytics4 extends AbstractAnalytic {
#config: AnalyticGoogleSettings;
#referrer: string;
_consentSettings?: ConsentSettings;

static get $dependencies(): Dependencies {
Expand Down Expand Up @@ -47,6 +48,8 @@ export class GoogleAnalytics4 extends AbstractAnalytic {

/**
* Initializes the Google Analytics 4 plugin.
* @param config
* @param {...any} rest
*/
constructor(
config: AnalyticGoogleSettings,
Expand All @@ -55,6 +58,7 @@ export class GoogleAnalytics4 extends AbstractAnalytic {
super(...rest);

this.#config = config;
this.#referrer = '';

this._analyticScriptName = 'google_analytics_4';

Expand Down Expand Up @@ -85,7 +89,9 @@ export class GoogleAnalytics4 extends AbstractAnalytic {
return;
}

this._ga4Script('event', 'page_view', this._getPageViewData(pageData));
const pageViewData = this._getPageViewData(pageData);
this._ga4Script('event', 'page_view', pageViewData);
this.#referrer = pageViewData.page_location;
}

/**
Expand Down Expand Up @@ -150,10 +156,17 @@ export class GoogleAnalytics4 extends AbstractAnalytic {
* Returns page view data derived from pageData param.
*/
_getPageViewData(pageData: Record<string, any>) {
const page_location = this._window.getUrl();
const clientDocument = this._window?.getDocument();
const page_referrer = this.#referrer || clientDocument?.referrer;

return {
page: pageData.path,
location: this._window.getUrl(),
title: document.title || '',
page_path: pageData.path,
page_location,
page_referrer,
page_route: pageData?.route?.getName() || '',
page_status: pageData?.response?.status,
page_title: clientDocument?.title || '',
};
}

Expand Down

0 comments on commit c69c207

Please sign in to comment.