Sha256: a708dac2541c47e80a8d6cc8023784c77e297d72d2fbdbb9b4ea5e1ebe440694

Contents?: true

Size: 1016 Bytes

Versions: 16

Compression:

Stored size: 1016 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 ?? []);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function gtag(..._arg): 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 ?? "{}"
    );
    const eventName = data["event"];
    delete data["event"];
    gtag("event", eventName, data);
  }

  recordPage(): void {
    gtag("event", "page_view", {
      page_title: document.title,
      page_location: document.location.toString(),
    });
  }
}

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
shimmer-0.0.41 src/controllers/analytics.ts
shimmer-0.0.40 src/controllers/analytics.ts
shimmer-0.0.39 src/controllers/analytics.ts
shimmer-0.0.38 src/controllers/analytics.ts
shimmer-0.0.37 src/controllers/analytics.ts
shimmer-0.0.36 src/controllers/analytics.ts
shimmer-0.0.35 src/controllers/analytics.ts
shimmer-0.0.34 src/controllers/analytics.ts
shimmer-0.0.33 src/controllers/analytics.ts
shimmer-0.0.32 src/controllers/analytics.ts
shimmer-0.0.31 src/controllers/analytics.ts
shimmer-0.0.30 src/controllers/analytics.ts
shimmer-0.0.29 src/controllers/analytics.ts
shimmer-0.0.28 src/controllers/analytics.ts
shimmer-0.0.27 src/controllers/analytics.ts
shimmer-0.0.26 src/controllers/analytics.ts