Skip to content

Commit

Permalink
feat() add sendClickEvent to the google-tag-manager
Browse files Browse the repository at this point in the history
  • Loading branch information
nobilo committed Jun 22, 2021
1 parent dc72f82 commit 0b3b9da
Showing 1 changed file with 22 additions and 54 deletions.
76 changes: 22 additions & 54 deletions src/google-tag-manager.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
const DATA_LAYER = 'dataLayer';
const VIRTUAL_FUNNEL_CLICK = 'VirtualFunnelClick';

/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any */
export interface GtmSettings {
apiKey: string,
applicationName: string
}

export default class BalGoogconstagManager {
export default class s {
applicationName: string;


public static isEnabled(): boolean {
return (window as any)[DATA_LAYER] != null;
}

public static load(settings: { apiKey: string }): void {
public static load(settings: GtmSettings): void {
if (!this.isEnabled()) {
this.runGtmScript(settings.apiKey);
} else {
Expand All @@ -35,57 +41,19 @@ export default class BalGoogconstagManager {
'https://www.googconstagmanager.com/gtm.js?id=' + apiKey + dl;
f.parentNode.insertBefore(j, f);
}
/* eslint-enable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call */

/*
export const sendPageChangeEvent = (route: Route) => {
if (route && routeMapping[route.name as string]) {
googleAnalyticsSendEvent({
event: 'VirtualPageview',
...routeMapping[route.name as string] as any,
products: [{
name: 'Bauversicherung',
id: '',
price: '',
brand: 'Unternehmenskunden',
category: 'Haftpflicht, Recht, Sachwerte',
variant: '',
}],
});
}
};
export const sendEnhancedEcommerceTransaction = (premium: string) => {
googleAnalyticsSendEvent({
event: 'Enhanced_Ecommerce_Transaction',
ecommerce: {
purchase: {
actionField: {
id: `LGTH-${(new Date()).getFullYear()}-${leftPadWithZeros(getRandomInt(1, 999999999), 9)}`,
affiliation: 'Baloise',
},
products: [{
id: '',
name: 'Bauversicherung',
price: premium,
brand: 'Unternehmenskunden',
category: 'Haftpflicht, Recht, Sachwerte',
variant: '',
quantity: 1,
}],
},
},
});
};
function googleAnalyticsSendEvent(event: any) {
const log = Vue.$createLogger('GoogleAnalytics');
if ((window as any).dataLayer) {
log.info(`Send event (${event.event}) to Google Analytics`);
(window as any).dataLayer.push(event);
} else {
log.info(`Could not send event (${event.event}) to Google Analytics`);
public sendClickEvent (label: string): void {
this.googleAnalyticsSendEvent({
event: VIRTUAL_FUNNEL_CLICK,
VirtualClickCategory: VIRTUAL_FUNNEL_CLICK,
VirtualClickAction: this.applicationName,
VirtualClickLabel: label,
},
);
};

private googleAnalyticsSendEvent(event: any): void {
if ((window as any)[DATA_LAYER]) {
(window as any)[DATA_LAYER].push(event);
}
}
*/
}
}

0 comments on commit 0b3b9da

Please sign in to comment.