Skip to content

Commit

Permalink
Merge pull request #159 from concord-consortium/184463878-update-to-g…
Browse files Browse the repository at this point in the history
…oogle-analytics-4

chore: Update to Google Analytics 4 (PT-184463878)
  • Loading branch information
emcelroy authored Jun 7, 2023
2 parents 1783c0f + 1a22bfd commit 79ef76b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 32 deletions.
8 changes: 4 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[submodule "vendor/jquery"]
path = vendor/jquery
url = git://github.com/jquery/jquery.git
url = https://github.com/jquery/jquery.git
[submodule "vendor/fonts/Font-Awesome"]
path = vendor/fonts/Font-Awesome
url = git://github.com/FortAwesome/Font-Awesome.git
url = https://github.com/FortAwesome/Font-Awesome.git
[submodule "vendor/lab-sensor-applet-interface"]
path = vendor/lab-sensor-applet-interface
url = git://github.com/concord-consortium/lab-sensor-applet-interface.git
url = https://github.com/concord-consortium/lab-sensor-applet-interface.git
[submodule "vendor/lab-sensor-applet-interface-dist"]
path = vendor/lab-sensor-applet-interface-dist
url = git://github.com/concord-consortium/lab-sensor-applet-interface-dist.git
url = https://github.com/concord-consortium/lab-sensor-applet-interface-dist.git
[submodule "vendor/lab-grapher"]
path = vendor/lab-grapher
url = https://github.com/concord-consortium/lab-grapher.git
Expand Down
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"esversion": 6,
"predef": ["$", "d3", "describe", "it", "beforeAll", "beforeEach", "afterEach", "sinon", "should", "expect", "_gaq"],
"predef": ["$", "d3", "describe", "it", "beforeAll", "beforeEach", "afterEach", "sinon", "should", "expect", "gtag"],
"browser" : true,
"eqnull": true,
"boss" : false,
Expand Down
2 changes: 1 addition & 1 deletion developer-doc/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Interactive **About** box.

### Runtime Google Analytics

If the global `_gaq` is defined, then Lab will send some events to Google Analytics. This is done
If the global `gtag` is defined, then Lab will send some events to Google Analytics. This is done
through trackEvent method in `src/lab/common/controllers/scripting-api.js`. So if you want this to happen then the
page embedding `lab.js` or `lab.min.js` should include the standard Google Analytics script setting things up.

Expand Down
17 changes: 8 additions & 9 deletions src/embeddable.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
<% } %>

<% if (htmlWebpackPlugin.options.gaAccountId) { %>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '<%= htmlWebpackPlugin.options.gaAccountId %>']);
_gaq.push(['_setAllowAnchor', true]);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<%= htmlWebpackPlugin.options.gaAccountId %>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', '<%= htmlWebpackPlugin.options.gaAccountId %>');
</script>
<% } %>

Expand Down
6 changes: 3 additions & 3 deletions src/embeddable.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global Lab, $, _gaq, Embeddable: true, AUTHORING: true */
/*global Lab, $, gtag, Embeddable: true, AUTHORING: true */
/*jshint boss:true */

// Strawman setting for telling the interactive to be in "author mode",
Expand All @@ -8,15 +8,15 @@ AUTHORING = false;
Embeddable = window.Embeddable || {};
Embeddable.sendGAPageview = function (){
// send the pageview to GA
if (typeof _gaq === 'undefined'){
if (typeof gtag === 'undefined'){
return;
}
// make an array out of the URL's hashtag string, splitting the string at every ampersand
var my_hashtag_array = location.hash.split('&');

// grab the first value of the array (assuming that's the value that indicates which interactive is being viewed)
var my_hashtag = my_hashtag_array[0];
_gaq.push(['_trackPageview', location.pathname + my_hashtag]);
gtag("event", "page_view", { "page_path": location.pathname + my_hashtag })
};

Embeddable.load = function(interactiveUrl, containerSelector, callbacks) {
Expand Down
17 changes: 8 additions & 9 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
<link href='//fonts.googleapis.com/css?family=Lato:300italic,700italic,300,400,400italic,700' rel='stylesheet' type='text/css'>
<link href='index.css' rel='stylesheet' type='text/css'>
<% if (htmlWebpackPlugin.options.gaAccountId) { %>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '<%= htmlWebpackPlugin.options.gaAccountId %>']);
_gaq.push(['_setAllowAnchor', true]);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<%= htmlWebpackPlugin.options.gaAccountId %>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', '<%= htmlWebpackPlugin.options.gaAccountId %>');
</script>
<% } %>
</head>
Expand Down
8 changes: 3 additions & 5 deletions src/lab/common/controllers/scripting-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,17 @@ export default function ScriptingAPI(interactivesController, model) {

/* Send a tracking event to Google Analytics */
function trackEvent(category, action, label) {
var googleAnalytics;

if (typeof _gaq === 'undefined') {
// console.error("Google Analytics not defined, Can not send trackEvent");
if (typeof gtag === 'undefined') {
// console.error("Google Analytics not defined, Can not send event");
return;
}
googleAnalytics = _gaq;
if (!category) {
category = "Interactive";
}
// console.log("Sending a track page event Google Analytics (category:action:label):");
// console.log("(" + category + ":" + action + ":" + label + ")");
googleAnalytics.push(['_trackEvent', category, action, label]);
gtag("event", action, { "event_category": category, "event_label": label });
}

return {
Expand Down

0 comments on commit 79ef76b

Please sign in to comment.