Skip to content

Commit

Permalink
Add more component analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood committed Feb 15, 2024
1 parent 95a7b6a commit 2600bf8
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/nationalarchives/analytics.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@ import {
valueGetters,
} from "./lib/analytics-helpers.mjs";
import BreadcrumbAnalytics from "./components/breadcrumbs/analytics.js";
import CheckboxesAnalytics from "./components/checkboxes/analytics.js";
import GlobalHeaderAnalytics from "./components/global-header/analytics.js";
import HeaderAnalytics from "./components/header/analytics.js";
import HeroAnalytics from "./components/hero/analytics.js";
import PictureAnalytics from "./components/picture/analytics.js";
import RadiosAnalytics from "./components/radios/analytics.js";
import SearchFieldAnalytics from "./components/search-field/analytics.js";
import TextInputAnalytics from "./components/text-input/analytics.js";
import TextareaAnalytics from "./components/textarea/analytics.js";

const componentAnalytics = [
...BreadcrumbAnalytics,
...CheckboxesAnalytics,
...GlobalHeaderAnalytics,
...HeaderAnalytics,
...HeroAnalytics,
...PictureAnalytics,
...RadiosAnalytics,
...SearchFieldAnalytics,
...TextInputAnalytics,
...TextareaAnalytics,
];

class EventTracker {
Expand Down Expand Up @@ -113,6 +123,10 @@ class EventTracker {
typeof eventDataInit.state === "function"
? eventDataInit.state.call(this, $el, $scope, event)
: eventDataInit.state || null,
group:
typeof eventDataInit.group === "function"
? eventDataInit.group.call(this, $el, $scope, event)
: eventDataInit.group || null,
scope: getXPathTo($scope),
targetElement: targetElement,
timestamp: new Date().toISOString(),
Expand Down
25 changes: 25 additions & 0 deletions src/nationalarchives/components/checkboxes/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { valueGetters } from "../../lib/analytics-helpers.mjs";

export default [
{
scope: ".tna-checkboxes",
areaName: "checkboxes",
events: [
{
eventName: "toggle",
targetElement: ".tna-checkboxes__item input",
on: "change",
data: {
state: valueGetters.checked,
// eslint-disable-next-line no-unused-vars
value: ($el, $scope, event) => $el.parentNode.innerText,
// eslint-disable-next-line no-unused-vars
group: ($el, $scope, event) =>
$scope
.closest(".tna-form__group")
?.querySelector(".tna-form__heading")?.innerText,
},
},
],
},
];
25 changes: 25 additions & 0 deletions src/nationalarchives/components/radios/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { valueGetters } from "../../lib/analytics-helpers.mjs";

export default [
{
scope: ".tna-radios",
areaName: "radios",
events: [
{
eventName: "toggle",
targetElement: ".tna-radios__item input",
on: "change",
data: {
state: valueGetters.checked,
// eslint-disable-next-line no-unused-vars
value: ($el, $scope, event) => $el.parentNode.innerText,
// eslint-disable-next-line no-unused-vars
group: ($el, $scope, event) =>
$scope
.closest(".tna-form__group")
?.querySelector(".tna-form__heading")?.innerText,
},
},
],
},
];
22 changes: 22 additions & 0 deletions src/nationalarchives/components/search-field/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { valueGetters } from "../../lib/analytics-helpers.mjs";

export default [
{
scope: ".tna-search-field",
areaName: "search-field",
events: [
{
eventName: "blurred",
targetElement: ".tna-search-field__input",
on: "blur",
data: {
// eslint-disable-next-line no-unused-vars
value: valueGetters.value,
// eslint-disable-next-line no-unused-vars
group: ($el, $scope, event) =>
$scope.querySelector(".tna-form__heading")?.innerText,
},
},
],
},
];
23 changes: 23 additions & 0 deletions src/nationalarchives/components/text-input/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { valueGetters } from "../../lib/analytics-helpers.mjs";

export default [
{
scope: ".tna-text-input",
areaName: "text-input",
events: [
{
eventName: "blurred",
on: "blur",
data: {
// eslint-disable-next-line no-unused-vars
value: valueGetters.value,
// eslint-disable-next-line no-unused-vars
group: ($el, $scope, event) =>
$scope
.closest(".tna-form__group")
?.querySelector(".tna-form__heading")?.innerText,
},
},
],
},
];
23 changes: 23 additions & 0 deletions src/nationalarchives/components/textarea/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { valueGetters } from "../../lib/analytics-helpers.mjs";

export default [
{
scope: ".tna-textarea",
areaName: "textarea",
events: [
{
eventName: "blurred",
on: "blur",
data: {
// eslint-disable-next-line no-unused-vars
value: valueGetters.value,
// eslint-disable-next-line no-unused-vars
group: ($el, $scope, event) =>
$scope
.closest(".tna-form__group")
?.querySelector(".tna-form__heading")?.innerText,
},
},
],
},
];
2 changes: 2 additions & 0 deletions src/nationalarchives/lib/analytics-helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const valueGetters = {
html: ($el, $scope, event) => $el.innerHTML,
// eslint-disable-next-line no-unused-vars
value: ($el, $scope, event) => $el.value,
// eslint-disable-next-line no-unused-vars
checked: ($el, $scope, event) => ($el.checked ? "checked" : "unchecked"),
};

export { getXPathTo, getClosestHeading, valueGetters };

0 comments on commit 2600bf8

Please sign in to comment.