Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions jstests/tests/test_analytics.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
define(function(require) {
"use strict";
var analytics = require("analytics");
var gamodule = require("gamodule");

QUnit.module("Google Analytics");
QUnit.test("test without analytics", function (assert) {
var result=[];
window.ga = function(cmd, id, auto) {
result[0] = [cmd, id, auto];
};
window.apidata.analytics = undefined;
var ga = analytics.init();
assert.equal(window.ga.l, undefined);
var ga = gamodule.init();
assert.notEqual(ga, undefined);
assert.equal(result.length, 0);
});

QUnit.test("test with analytics", function (assert) {
var result=[];
window.ga = function(cmd, id, auto) {
result[0] = [cmd, id, auto];
};
window.apidata.analytics = {"tracking_id": "X"};
var ga = analytics.init();
assert.notEqual(window.ga.l, undefined);
var ga = gamodule.init();
assert.notEqual(ga, undefined);
assert.equal(result[0][0], "create");
assert.equal(result[0][1], "X");
assert.equal(result[0][2], "auto");
});
});
33 changes: 0 additions & 33 deletions remoteappmanager/static/js/analytics.js

This file was deleted.

26 changes: 26 additions & 0 deletions remoteappmanager/static/js/gamodule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// This module contains the setup for google analytics.
// MUST not be renamed to analytics. Some blockers rely on name
// matching to prevent loading.
define([
], function () {
"use strict";

function init() {
var module;

if (window.apidata.analytics !== undefined) {
window.ga('create', window.apidata.analytics.tracking_id, 'auto');
} else {
window.ga = function () {

@martinRenou martinRenou Mar 29, 2017

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think window.ga = function () { return; } and window.ga = function () {}; will have the same effect but for the second option jslint would complain about empty brackets

@stefanoborini stefanoborini Mar 30, 2017

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pycharm complains for the exact opposite reason. It claims return is unnecessary.
by the way, I use jshint, not jslint (jslint is the one by Douglas Crockford IIRC, and while I respect the guy, I think his linter is a bit excessive)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahah ok :)

};
}
module = function () {
window.ga.apply(this, arguments);
};
return module;
}

return {
init: init
};
});
6 changes: 3 additions & 3 deletions remoteappmanager/static/js/home/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require([
"jquery",
"urlutils",
"dialogs",
"analytics",
"gamodule",
"home/models",
"home/views/application_list_view",
"home/views/application_view",
Expand All @@ -14,7 +14,7 @@ require([
$,
urlutils,
dialogs,
analytics,
gamodule,
models,
application_list_view,
application_view,
Expand All @@ -23,7 +23,7 @@ require([
init) {
"use strict";

var ga = analytics.init();
var ga = gamodule.init();
init.handlebars();

// This model keeps the retrieved content from the REST query locally.
Expand Down
12 changes: 12 additions & 0 deletions remoteappmanager/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@
{% endblock %}

{% block script %}
<script type="text/javascript">
// This will always be executed and create the basic ga function that
// does nothing
window.ga = function () {
(window.ga.q = window.ga.q || []).push(arguments);
};
window.ga.l = 1 * new Date();
window.GoogleAnalyticsObject = "ga";
</script>
{% if analytics %}
<script src='https://www.google-analytics.com/analytics.js' async type="text/javascript"></script>
{% endif %}
<script src="{{static_url("components/requirejs/require.js") }}" type="text/javascript" charset="utf-8"></script>
{% endblock %}

Expand Down