shimmer 0.0.19 → 0.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: adda5b02ed66f485505f07b24ba314621c5b0dd1cae9ce3093681651f0a1a509
4
- data.tar.gz: 89c1baa878afdf2bb38c2cc62eeafd298f15916f11466e5ec1e31eb5b42f9be6
3
+ metadata.gz: d8819816445402b9ba5fc3a8ffcd9e273f85b34018cd29d0e0c99b5a338e6c37
4
+ data.tar.gz: 91786fdab270afb2c5cd89c3874284dd1701263eacbd309043ba35b87ae49ad5
5
5
  SHA512:
6
- metadata.gz: f2add223e56adabf705dcb3061950ea47613d50caf4b7c240b3c7aa1907dbb0ef52b179b409183d2d205f21789d3cc15719e0ade1ba17387085eb47f9e2945d1
7
- data.tar.gz: e496ccb035f571cd070c2af88968d0ab6d9d45bcf23016655a9a7213beccf9c83907f001f2f26c4188ce5ce1bc4affbd5c29d2b16d43d60a7949df0e9446f908
6
+ metadata.gz: 0f0abe61b9c8a001df68bfd019cee02d5de58c2c582ab58ed0f6899dcad78409f9136e62cd5892be9dc5167708f19b04df66afbdaccf0f925e5b986f1e9de97b
7
+ data.tar.gz: 2f8031509918cd09d64dd87158b9b119fc90f3e594c2fbdc068898bdffe29b5687cb7f6161af7d7d98061fe17ad72747388c8a01e2c381f082801d02ace39193
@@ -5,4 +5,5 @@
5
5
  "editor.formatOnSave": false,
6
6
  "editor.defaultFormatter": "castwide.solargraph"
7
7
  },
8
+ "typescript.tsdk": "node_modules/typescript/lib"
8
9
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Shimmer
4
- VERSION = "0.0.19"
4
+ VERSION = "0.0.20"
5
5
  end
data/src/consent.ts CHANGED
@@ -1,12 +1,24 @@
1
1
  import { getCookie, setCookie } from "./util";
2
2
 
3
+ const consentCategories = ["essential", "targeting", "statistic"] as const;
4
+ export type ConsentCategory = typeof consentCategories[number];
5
+
3
6
  export class Consent {
4
- get permitted(): string[] {
5
- return (getCookie("consent") ?? "").split(",");
7
+ #consentListeners: Record<ConsentCategory, Array<() => void>> = {
8
+ essential: [],
9
+ targeting: [],
10
+ statistic: [],
11
+ };
12
+
13
+ get permitted(): ConsentCategory[] {
14
+ return (getCookie("consent") ?? "").split(",") as ConsentCategory[];
6
15
  }
7
16
 
8
- set permitted(settings: string[]) {
17
+ set permitted(settings: ConsentCategory[]) {
9
18
  setCookie("consent", settings.join(","));
19
+ settings.forEach((category) => {
20
+ this.#consentListeners[category].forEach((e) => e());
21
+ });
10
22
  }
11
23
 
12
24
  get given(): boolean {
@@ -14,7 +26,7 @@ export class Consent {
14
26
  }
15
27
 
16
28
  permitAll(): void {
17
- this.permitted = ["essential", "targeting", "statistic"];
29
+ this.permitted = consentCategories.slice();
18
30
  }
19
31
 
20
32
  denyAll(): void {
@@ -25,4 +37,28 @@ export class Consent {
25
37
  const element = document.getElementById("personalization-settings");
26
38
  if (element) element.hidden = false;
27
39
  }
40
+
41
+ consentFor(category: ConsentCategory): Promise<void> {
42
+ return new Promise((res) => {
43
+ if (this.permitted.includes(category)) {
44
+ res();
45
+ } else {
46
+ this.#consentListeners[category].push(res);
47
+ }
48
+ });
49
+ }
50
+
51
+ async enableGoogleAnalytics(
52
+ id: string,
53
+ role: ConsentCategory = "statistic"
54
+ ): Promise<void> {
55
+ await this.consentFor(role);
56
+ window.gtag("config", id);
57
+ const script = document.createElement("script");
58
+ script.setAttribute(
59
+ "src",
60
+ `https://www.googletagmanager.com/gtag/js?id=${id}`
61
+ );
62
+ document.head.appendChild(script);
63
+ }
28
64
  }
@@ -0,0 +1,42 @@
1
+ import { Controller } from "@hotwired/stimulus";
2
+
3
+ interface DataEvent {
4
+ event: string;
5
+ }
6
+
7
+ declare global {
8
+ interface Window {
9
+ dataLayer?: DataEvent[];
10
+ gtag(...arg): void;
11
+ }
12
+ }
13
+
14
+ const dataLayer = (window.dataLayer = window.dataLayer ?? []);
15
+
16
+ window.gtag = (...arg) => {
17
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
+ dataLayer.push(arg as any);
19
+ };
20
+ window.gtag("js", new Date());
21
+
22
+ export default class extends Controller {
23
+ connect(): void {
24
+ this.recordPage();
25
+ }
26
+
27
+ recordAction(event: MouseEvent): void {
28
+ const data = JSON.parse(
29
+ (event.target as HTMLElement).dataset.analytics ?? "{}"
30
+ );
31
+ dataLayer.push(data);
32
+ }
33
+
34
+ recordPage(): void {
35
+ const event = {
36
+ event: "Pageview",
37
+ path: location.pathname + location.search,
38
+ host: location.host,
39
+ };
40
+ dataLayer.push(event);
41
+ }
42
+ }
@@ -1,4 +1,5 @@
1
1
  import { Controller } from "@hotwired/stimulus";
2
+ import { ConsentCategory } from "../consent";
2
3
 
3
4
  export default class extends Controller {
4
5
  static targets = ["check"];
@@ -7,7 +8,7 @@ export default class extends Controller {
7
8
  connect(): void {
8
9
  this.checkTargets.forEach((input) => {
9
10
  input.checked =
10
- window.ui?.consent.permitted.includes(input.name) ||
11
+ window.ui?.consent.permitted.includes(input.name as ConsentCategory) ||
11
12
  input.name === "essential";
12
13
  });
13
14
  }
@@ -29,7 +30,7 @@ export default class extends Controller {
29
30
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
30
31
  window.ui!.consent.permitted = this.checkTargets
31
32
  .filter((e) => e.checked)
32
- .map((e) => e.name);
33
+ .map((e) => e.name) as ConsentCategory[];
33
34
  this.closeAll();
34
35
  }
35
36
 
data/src/index.ts CHANGED
@@ -4,6 +4,7 @@ import { PopoverPresenter } from "./popover";
4
4
  import { Consent } from "./consent";
5
5
  import RemoteNavigationController from "./controllers/remote-navigation";
6
6
  import ConsentController from "./controllers/consent";
7
+ import AnalyticsController from "./controllers/analytics";
7
8
  import "./touch";
8
9
 
9
10
  export { registerServiceWorker } from "./serviceworker";
@@ -19,6 +20,12 @@ declare global {
19
20
  }
20
21
  }
21
22
 
23
+ window.ui = {
24
+ modal: new ModalPresenter(),
25
+ popover: new PopoverPresenter(),
26
+ consent: new Consent(),
27
+ };
28
+
22
29
  function createRemoteDestination(): void {
23
30
  if (document.getElementById("shimmer")) {
24
31
  return;
@@ -37,9 +44,7 @@ export async function start({
37
44
  createRemoteDestination();
38
45
  application.register("remote-navigation", RemoteNavigationController);
39
46
  application.register("consent", ConsentController);
40
- window.ui = {
41
- modal: new ModalPresenter(),
42
- popover: new PopoverPresenter(),
43
- consent: new Consent(),
44
- };
47
+ application.register("analytics", AnalyticsController);
45
48
  }
49
+
50
+ export const ui = window.ui;
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shimmer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.19
4
+ version: 0.0.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Ravens
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-12 00:00:00.000000000 Z
11
+ date: 2022-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -165,6 +165,7 @@ files:
165
165
  - package.json
166
166
  - rollup.config.js
167
167
  - src/consent.ts
168
+ - src/controllers/analytics.ts
168
169
  - src/controllers/consent.ts
169
170
  - src/controllers/remote-navigation.ts
170
171
  - src/index.ts