Sha256: ee1565065813e09b316e5c7e23417852a84e40a159538f8d7e019f1fc2b78a1e

Contents?: true

Size: 903 Bytes

Versions: 4

Compression:

Stored size: 903 Bytes

Contents

import { Controller } from "@hotwired/stimulus";

interface DataEvent {
  event: string;
}

declare global {
  interface Window {
    dataLayer?: DataEvent[];
    gtag(...arg): void;
  }
}

const dataLayer = (window.dataLayer = window.dataLayer ?? []);
function gtag(): void {
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore
  // eslint-disable-next-line prefer-rest-params
  dataLayer.push(arguments);
}
window.gtag = gtag;

export default class extends Controller {
  connect(): void {
    this.recordPage();
  }

  recordAction(event: MouseEvent): void {
    const data = JSON.parse(
      (event.target as HTMLElement).dataset.analytics ?? "{}"
    );
    dataLayer.push(data);
  }

  recordPage(): void {
    const event = {
      event: "Pageview",
      path: location.pathname + location.search,
      host: location.host,
    };
    dataLayer.push(event);
  }
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
shimmer-0.0.25 src/controllers/analytics.ts
shimmer-0.0.24 src/controllers/analytics.ts
shimmer-0.0.23 src/controllers/analytics.ts
shimmer-0.0.22 src/controllers/analytics.ts