improve web sdk

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-02-12 09:35:18 +01:00
parent 5afc49b7e4
commit a1fea7eb98
5 changed files with 22 additions and 14 deletions

View File

@@ -13,12 +13,18 @@ export class MixanWeb extends Mixan<MixanWebOptions> {
constructor(options: MixanWebOptions) {
super(options);
if (this.options.trackOutgoingLinks) {
this.trackOutgoingLinks();
}
if (!this.isServer()) {
this.setGlobalProperties({
referrer: document.referrer,
});
if (this.options.trackScreenViews) {
this.trackScreenViews();
if (this.options.trackOutgoingLinks) {
this.trackOutgoingLinks();
}
if (this.options.trackScreenViews) {
this.trackScreenViews();
}
}
}
@@ -33,12 +39,17 @@ export class MixanWeb extends Mixan<MixanWebOptions> {
document.addEventListener('click', (event) => {
const target = event.target as HTMLElement;
if (target.tagName === 'A') {
const href = target.getAttribute('href');
const link = target.closest('a');
if (link && target) {
const href = link.getAttribute('href');
if (href?.startsWith('http')) {
super.event('link_out', {
href,
text: target.innerText,
text:
link.innerText ||
link.getAttribute('title') ||
target.getAttribute('alt') ||
target.getAttribute('title'),
});
}
}
@@ -95,7 +106,6 @@ export class MixanWeb extends Mixan<MixanWebOptions> {
...(properties ?? {}),
path,
title: document.title,
referrer: document.referrer,
});
}
}