Skip to content

Commit 6892995

Browse files
committed
update Google Analytics
1 parent 8dcead5 commit 6892995

File tree

7 files changed

+39
-32
lines changed

7 files changed

+39
-32
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
3.9.3 (not yet released)
1+
3.9.3 (2023-06-26)
22
-------------------------
3+
* Update for Google Analytics 4 (GA4)
34
* #812 don't throw an error when attempting to report an error fetching config data
45
* #799 Deprecate / remove DOMUtil (should be invisible but simplifies code)
56

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@knight-lab/timelinejs",
3-
"version": "3.9.2",
3+
"version": "3.9.3",
44
"license": "MPL-2.0",
55
"description": "TimelineJS v3: A Storytelling Timeline built in JavaScript, made by Northwestern University Knight Lab.",
66
"dependencies": {

src/embed/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
var options = optionsFromUrlParams();
151151
createEmbedDiv('timeline-embed', options.width, options.height);
152152
// ga_property_id is not something we let users override
153-
options.ga_property_id = 'UA-27829802-4';
153+
options.ga_measurement_id = 'G-LVEFKMG087'
154154
if (typeof(options.source) == 'undefined') {
155155
options.source = '1xuY4upIooEeszZ_lCmeNx24eSFWe0rHe9ZdqH2xqVNk' // women in computing
156156
}

src/js/timeline/Timeline.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class Timeline {
133133
slide_default_fade: "0%", // landscape fade
134134
zoom_sequence: [0.5, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89], // Array of Fibonacci numbers for TimeNav zoom levels
135135
language: "en",
136+
ga_measurement_id: null,
136137
ga_property_id: null,
137138
track_events: ['back_to_start', 'nav_next', 'nav_previous', 'zoom_in', 'zoom_out'],
138139
theme: null,
@@ -991,31 +992,23 @@ class Timeline {
991992
this._el.container.focus();
992993
}
993994

994-
_initGoogleAnalytics() {
995-
(function(i, s, o, g, r, a, m) {
996-
i['GoogleAnalyticsObject'] = r;
997-
i[r] = i[r] || function() {
998-
(i[r].q = i[r].q || []).push(arguments)
999-
}, i[r].l = 1 * new Date();
1000-
a = s.createElement(o), m = s.getElementsByTagName(o)[0];
1001-
a.async = 1;
1002-
a.src = g;
1003-
m.parentNode.insertBefore(a, m)
1004-
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
1005-
1006-
ga('create', this.options.ga_property_id, 'auto');
1007-
ga('set', 'anonymizeIp', true);
995+
_initGoogleAnalytics(measurement_id) {
996+
loadJS(`https://www.googletagmanager.com/gtag/js?id=${measurement_id}`)
997+
window.dataLayer = window.dataLayer || [];
998+
window.gtag = function(){dataLayer.push(arguments);}
999+
gtag('js', new Date());
1000+
gtag('config', measurement_id);
10081001
}
10091002

10101003
_initAnalytics() {
1011-
if (this.options.ga_property_id === null) { return; }
1012-
this._initGoogleAnalytics();
1013-
ga('send', 'pageview');
1004+
let measurement_id = this.options.ga_measurement_id || this.options.ga_property_id || null;
1005+
if (!measurement_id) { return; }
1006+
this._initGoogleAnalytics(measurement_id);
10141007
var events = this.options.track_events;
10151008
for (let i = 0; i < events.length; i++) {
10161009
var event_ = events[i];
10171010
this.addEventListener(event_, function(e) {
1018-
ga('send', 'event', e.type, 'clicked');
1011+
gtag('event', e.type);
10191012
});
10201013
}
10211014
}

src/template/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
hash_bookmark: true,
1414
start_at_end: false,
1515
autolink: false,
16+
ga_measurement_id: 'G-LVEFKMG087',
1617
sheets_proxy: 'https://sheets-proxy.knightlab.com/proxy/'
1718
}
1819

website/templates/_analytics.html

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
<!-- Global site tag (gtag.js) - Google Analytics -->
2-
<!-- This fragment is only used on some of the examples pages -->
3-
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-537357-20"></script>
4-
<script>
5-
window.dataLayer = window.dataLayer || [];
6-
function gtag(){dataLayer.push(arguments);}
7-
gtag('js', new Date());
8-
9-
gtag('config', 'UA-537357-20', { 'anonymize_ip': true });
10-
</script>
1+
<!-- Google tag (gtag.js) -->
2+
<!-- This fragment is only used on some of the examples pages -->
3+
<script async src="https://www.googletagmanager.com/gtag/js?id=G-LVEFKMG087"></script>
4+
<script>
5+
window.dataLayer = window.dataLayer || [];
6+
function gtag(){dataLayer.push(arguments);}
7+
gtag('js', new Date());
8+
gtag('config', 'G-LVEFKMG087');
9+
</script>

website/templates/docs/options.html

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,19 @@ <h2 class="product-header" id="options">TimelineJS options</h2>
449449
</div>
450450
</div>
451451

452+
<div class="grid">
453+
454+
<div class="column-2 column-6-phone">
455+
<code>ga_measurement_id</code>
456+
</div>
457+
<div class="column-2 column-6-phone">
458+
null
459+
</div>
460+
<div class="column-8">
461+
Google Measurement ID. For self-hosted timelines, if this value is set, then Google Analytics 4 will be initialized and pageviews and other navigation events will be logged to the given measurement ID. For Knight Lab hosted timelines, this cannot be overridden with your analytics ID.
462+
</div>
463+
</div>
464+
452465
<div class="grid">
453466

454467
<div class="column-2 column-6-phone">
@@ -458,7 +471,7 @@ <h2 class="product-header" id="options">TimelineJS options</h2>
458471
null
459472
</div>
460473
<div class="column-8">
461-
Google Analytics ID.
474+
Google Analytics ID. This option is deprecated in favor of <code>ga_measurement_id</code> which better matches Google Analytics 4 (GA4) language. For now, if <code>ga_property_id</code> is set and <code>ga_measurement_id</code> is not, then <code>ga_property_id</code> will be used, even though the code is designed for GA4.
462475
</div>
463476
</div>
464477

0 commit comments

Comments
 (0)