Skip to content

Commit 580a08f

Browse files
authored
Merge pull request #13 from mcclatchy/zone-tracking
Zone tracking
2 parents 43037d2 + 9a56440 commit 580a08f

File tree

5 files changed

+74
-44
lines changed

5 files changed

+74
-44
lines changed

config/story.json

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,6 @@
22
"base": ".story-body [id^=zone-el]",
33
"ignore": ["zone-el-16"],
44
"zones": [
5-
{
6-
"id": "zone-moneycom",
7-
"filters": [
8-
{
9-
"type": "config",
10-
"name": "zone.moneycom",
11-
"value": true
12-
},
13-
{
14-
"type": "config",
15-
"name": "articleCredit",
16-
"pattern": "(Paradise Media|Bankrate)",
17-
"value": false
18-
}
19-
],
20-
"zephr": {
21-
"feature": "zone-moneycom"
22-
},
23-
"placement": {
24-
"type": "after",
25-
"value": "zone-el-101"
26-
}
27-
},
285
{
296
"id": "zone-taboola-recommendations",
307
"filters": [
@@ -46,7 +23,8 @@
4623
"placement": {
4724
"type": "after",
4825
"value": "zone-el-101"
49-
}
26+
},
27+
"tracking": true
5028
},
5129
{
5230
"id": "zone-si-tickets",

demo/story/index.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!doctype html>
22
<html lang="en" data-pagetype="story">
33
<head>
4-
<title>Zones section demo</title>
4+
<title>Zones story demo</title>
55
<link rel="icon" type="image/x-icon" href="https://www.kansascity.com/favicon.ico">
66

77
<meta charset="UTF-8">
@@ -19,24 +19,24 @@
1919
import distributeZones from "/index.js";
2020

2121
// Mimic the Yozons implementation
22-
distributeZones(locker).then(msg => { console.log(msg) });
22+
distributeZones(locker);
2323

2424
// Listen for the loaded event
2525
window.addEventListener("zones-loaded", console.log("zones are loaded"));
2626

2727
// Listen for the intersection event
28-
window.addEventListener("zone", (e) => console.log("%s showing", e.detail.id));
28+
window.addEventListener("zone", (e) => console.log("zone showing: %s", JSON.stringify(e.detail)));
29+
30+
// Listen for tracking events
31+
window.addEventListener("trackcustomzones", (e) => console.log("zone tracked: %s", JSON.stringify(e.detail)));
2932

3033
// Demo-only show vips
3134
import { getValidInsertionPoints } from "/lib/zones.js";
32-
const vips = getValidInsertionPoints();
33-
vips.forEach(ele => ele.classList.add("vip"));
35+
getValidInsertionPoints().forEach(ele => ele.classList.add("vip"));
3436
</script>
3537
</head>
3638

3739
<body>
38-
<!-- <div id="zone-el-1"></div> -->
39-
<!-- <div id="zone-el-2"></div> -->
4040
<div class="card flag"></div>
4141

4242
<article class="paper story-body">

lib/config.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ export function load(url) {
3131
// Loop through changes
3232
const zones = data.zones || [];
3333
zones.forEach(config => {
34-
let check = true;
34+
let show = true;
3535

3636
// Filter check
3737
if(config.filters) {
38-
check = config.filters.every(f => {
38+
show = config.filters.every(f => {
3939
const { type, name, value, pattern } = f;
4040

4141
switch(type) {
@@ -61,7 +61,7 @@ export function load(url) {
6161
}
6262

6363
// Render
64-
if(check) {
64+
if(show) {
6565
const zone = new Zone(config.id);
6666

6767
// Zephr connection
@@ -106,6 +106,11 @@ export function load(url) {
106106
zone.dataset.type = config.type;
107107
}
108108

109+
// Add tracking
110+
if(config.tracking) {
111+
zone.tracking = true;
112+
}
113+
109114
// Placement
110115
if(config.placement) {
111116
const {type, value} = config.placement;

lib/zones.js

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
// Internals
66
let map = new Map();
77
let fragment = new DocumentFragment();
8-
let observer = new IntersectionObserver(handleIntersection, { rootMargin: "500px" })
8+
let loadObserver = new IntersectionObserver(handleLoad, { rootMargin: "500px" });
9+
let trackingObserver = new IntersectionObserver(handleTracking, { threshold: 0.25 });
910

1011
// Locker
1112
export let locker;
@@ -55,6 +56,18 @@ export class Zone {
5556
map.set(val, this);
5657
}
5758

59+
/*
60+
* Getter/Setter for tracking
61+
*/
62+
63+
get tracking() {
64+
return this.element.dataset.tracking;
65+
}
66+
67+
set tracking(val) {
68+
this.element.dataset.tracking = val;
69+
}
70+
5871
/*
5972
* Getters for element attributes
6073
*/
@@ -153,7 +166,12 @@ export class Zone {
153166
this.log(this.msg || "zone moved into new location");
154167

155168
// Observer for performance team
156-
observer.observe(this.element);
169+
loadObserver.observe(this.element);
170+
171+
// Tracking observer
172+
if(this.tracking) {
173+
trackingObserver.observe(this.element);
174+
}
157175
}
158176
}
159177

@@ -394,24 +412,53 @@ export function render() {
394412
}
395413

396414
/*
397-
* IntersectionObserver callback
415+
* Intersection callback
398416
*/
399417

400-
function handleIntersection(entries, observer) {
418+
function handleLoad(entries, observer) {
401419
entries.forEach((entry) => {
402420
if(entry.isIntersecting) {
403-
// Build the event
404-
const loadZone = new CustomEvent("zone", {
421+
// Build the load event
422+
const intersectionEvent = new CustomEvent("zone", {
405423
detail: {
406424
id: entry.target.id
407425
}
408426
});
409427

410428
// Send it
411-
window.dispatchEvent(loadZone);
429+
window.dispatchEvent(intersectionEvent);
430+
431+
// Only do this once
432+
loadObserver.unobserve(entry.target);
433+
}
434+
});
435+
}
436+
437+
/*
438+
* Tracking callback
439+
*/
440+
441+
function handleTracking(entries, observer) {
442+
entries.forEach((entry) => {
443+
if(entry.isIntersecting) {
444+
// Build the tracking event if configured (Joe Grubbs spec)
445+
const trackEvent = new CustomEvent('trackcustomzones', {
446+
detail: {
447+
data: [
448+
{
449+
zone: entry.target.id.replace(/zone-(el-)?/, ''),
450+
action: "impression",
451+
count: 1
452+
}
453+
]
454+
}
455+
});
456+
457+
// Send it
458+
window.dispatchEvent(trackEvent);
412459

413-
// Only do it once
414-
observer.unobserve(entry.target);
460+
// Only do this once
461+
trackingObserver.unobserve(entry.target);
415462
}
416463
});
417464
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mcclatchy/zones",
3-
"version": "1.5.3",
3+
"version": "1.6.0",
44
"description": "A Yozons extension to dynamically distribute or re-distribute zones on the websites.",
55
"main": "index.js",
66
"directories": {

0 commit comments

Comments
 (0)