Move all the trackers to a separate js pack

This commit is contained in:
Paul Chavard 2019-03-26 16:02:08 +01:00
parent b8c309acdb
commit e71cdcd12c
22 changed files with 216 additions and 174 deletions

View file

@ -5,7 +5,8 @@ module.exports = {
sourceType: 'module'
},
globals: {
'process': true
'process': true,
'gon': true
},
plugins: ['prettier'],
extends: ['eslint:recommended', 'prettier'],

View file

@ -28,6 +28,7 @@ gem 'dotenv-rails', require: 'dotenv/rails-now' # dotenv should always be loaded
gem 'flipflop'
gem 'fog-openstack'
gem 'font-awesome-rails'
gem 'gon'
gem 'groupdate'
gem 'haml-rails'
gem 'hashie'

View file

@ -224,6 +224,10 @@ GEM
formatador (0.2.5)
globalid (0.4.2)
activesupport (>= 4.2.0)
gon (6.2.1)
actionpack (>= 3.0)
multi_json
request_store (>= 1.0)
groupdate (4.1.1)
activesupport (>= 4.2)
guard (2.15.0)
@ -686,6 +690,7 @@ DEPENDENCIES
flipflop
fog-openstack
font-awesome-rails
gon
groupdate
guard
guard-livereload

View file

@ -15,6 +15,7 @@ class ApplicationController < ActionController::Base
before_action :staging_authenticate
before_action :set_active_storage_host
before_action :setup_tracking
def staging_authenticate
if StagingAuthService.enabled? && !authenticate_with_http_basic { |username, password| StagingAuthService.authenticate(username, password) }
@ -89,6 +90,15 @@ class ApplicationController < ActionController::Base
ActiveStorage::Current.host = request.base_url
end
def setup_tracking
gon.matomo = matomo_config
gon.sentry = sentry_config
if administrateur_signed_in?
gon.sendinblue = sendinblue_config
end
end
def logged_users
@logged_users ||= [
current_user,
@ -190,4 +200,50 @@ class ApplicationController < ActionController::Base
true
end
end
def sentry_config
sentry = Rails.application.secrets.sentry
{
key: sentry[:client_key],
enabled: sentry[:enabled],
user: {
id: current_user&.id,
email: current_email
}
}
end
def matomo_config
matomo = Rails.application.secrets.matomo
{
key: matomo[:client_key],
enabled: matomo[:enabled]
}
end
def sendinblue_config
sendinblue = Rails.application.secrets.sendinblue
{
key: sendinblue[:client_key],
enabled: sendinblue[:enabled],
administrateur: {
email: current_administrateur&.email,
payload: {
DS_SIGN_IN_COUNT: current_administrateur&.sign_in_count,
DS_CREATED_AT: current_administrateur&.created_at,
DS_ACTIVE: current_administrateur&.active,
DS_ID: current_administrateur&.id
}
}
}
end
def current_email
current_user&.email ||
current_gestionnaire&.email ||
current_administrateur&.email
end
end

View file

@ -106,17 +106,4 @@ module ApplicationHelper
root_path
end
end
def sentry_config
sentry = Rails.application.secrets.sentry
if sentry
{
dsn: sentry[:browser],
id: current_user&.id,
email: current_email
}.to_json
else
{}
end
end
end

View file

@ -5,7 +5,6 @@ import * as ActiveStorage from 'activestorage';
import jQuery from 'jquery';
import '../shared/activestorage/ujs';
import '../shared/sentry';
import '../shared/rails-ujs-fix';
import '../shared/safari-11-file-xhr-workaround';
import '../shared/autocomplete';

View file

@ -6,7 +6,6 @@ import Chartkick from 'chartkick';
import Highcharts from 'highcharts';
import '../shared/activestorage/ujs';
import '../shared/sentry';
import '../shared/rails-ujs-fix';
import '../shared/safari-11-file-xhr-workaround';
import '../shared/autocomplete';

View file

@ -0,0 +1 @@
import '../shared/track/sendinblue';

View file

@ -0,0 +1,2 @@
import '../shared/track/matomo';
import '../shared/track/sentry';

View file

@ -1,13 +0,0 @@
const _DATA = {};
function setupData() {
if (window.DATA.length) {
Object.assign(_DATA, ...window.DATA);
window.DATA.length = 0;
}
}
export function getData(namespace) {
setupData();
return namespace ? _DATA[namespace] : _DATA;
}

View file

@ -1,14 +0,0 @@
import { init, configureScope } from '@sentry/browser';
import { getData } from './data';
const { dsn, email, id } = getData('sentry');
if (dsn) {
init({ dsn });
if (email) {
configureScope(scope => {
scope.setUser({ id, email });
});
}
}

View file

@ -0,0 +1,47 @@
const { key, enabled } = gon.matomo || {};
if (enabled) {
window._paq = window._paq || [];
const url = '//stats.data.gouv.fr/';
const trackerUrl = `${url}piwik.php`;
const jsUrl = `${url}piwik.js`;
// Configure Matomo analytics
window._paq.push(['setCookieDomain', '*.www.demarches-simplifiees.fr']);
window._paq.push(['setDomains', ['*.www.demarches-simplifiees.fr']]);
window._paq.push(['setDoNotTrack', true]);
window._paq.push(['trackPageView']);
window._paq.push(['enableLinkTracking']);
// Load script from Matomo
window._paq.push(['setTrackerUrl', trackerUrl]);
window._paq.push(['setSiteId', key]);
const script = document.createElement('script');
const firstScript = document.getElementsByTagName('script')[0];
script.type = 'text/javascript';
script.id = 'matomo-js';
script.async = true;
script.src = jsUrl;
firstScript.parentNode.insertBefore(script, firstScript);
// Send Matomo a new event when navigating to a new page using Turbolinks
// (see https://developer.matomo.org/guides/spa-tracking)
let previousPageUrl = null;
addEventListener('turbolinks:load', event => {
if (previousPageUrl) {
window._paq.push(['setReferrerUrl', previousPageUrl]);
window._paq.push(['setCustomUrl', window.location.href]);
window._paq.push(['setDocumentTitle', document.title]);
if (event.data && event.data.timing) {
window._paq.push([
'setGenerationTimeMs',
event.data.timing.visitEnd - event.data.timing.visitStart
]);
}
window._paq.push(['trackPageView']);
}
previousPageUrl = window.location.href;
});
}

View file

@ -0,0 +1,22 @@
const { key, enabled, administrateur } = gon.sendinblue || {};
if (enabled) {
window.sib = {
equeue: [],
client_key: key,
email_id: administrateur.email
};
const script = document.createElement('script');
const firstScript = document.getElementsByTagName('script')[0];
script.type = 'text/javascript';
script.id = 'sendinblue-js';
script.async = true;
script.src = `https://sibautomation.com/sa.js?key=${window.sib.client_key}`;
firstScript.parentNode.insertBefore(script, firstScript);
window.sib.equeue.push({ page: [] });
window.sib.equeue.push({
identify: [administrateur.email, administrateur.payload]
});
}

View file

@ -0,0 +1,14 @@
import * as Sentry from '@sentry/browser';
const { key, enabled, user } = gon.sentry || {};
// We need to check for key presence here as we do not have a dsn for browser yet
if (enabled && key) {
Sentry.init({ dsn: key });
if (user.email) {
Sentry.configureScope(scope => {
scope.setUser(user);
});
}
}

View file

@ -1,35 +0,0 @@
:javascript
var _paq = _paq || [];
// Configure Matomo analytics
_paq.push(["setCookieDomain", "*.www.demarches-simplifiees.fr"]);
_paq.push(["setDomains", ["*.www.demarches-simplifiees.fr"]]);
_paq.push(["setDoNotTrack", true]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
// Load script from Matomo
(function() {
var u="//stats.data.gouv.fr/";
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', '73']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
// Send Matomo a new event when navigating to a new page using Turbolinks
// (see https://developer.matomo.org/guides/spa-tracking)
(function() {
var previousPageUrl = null;
addEventListener('turbolinks:load', function(event) {
if (previousPageUrl) {
_paq.push(['setReferrerUrl', previousPageUrl]);
_paq.push(['setCustomUrl', window.location.href]);
_paq.push(['setDocumentTitle', document.title]);
if (event.data && event.data.timing) {
_paq.push(['setGenerationTimeMs', event.data.timing.visitEnd - event.data.timing.visitStart]);
}
_paq.push(['trackPageView']);
}
previousPageUrl = window.location.href;
});
})();

View file

@ -1,14 +0,0 @@
:javascript
(function() {
window.sib = { equeue: [], client_key: "#{Rails.application.secrets.sendinblue[:client_key]}" };
/* OPTIONAL: email for identify request*/
window.sib.email_id = '#{current_administrateur.email}';
window.sendinblue = {}; for (var j = ['track', 'identify', 'trackLink', 'page'], i = 0; i < j.length; i++) { (function(k) { window.sendinblue[k] = function() { var arg = Array.prototype.slice.call(arguments); (window.sib[k] || function() { var t = {}; t[k] = arg; window.sib.equeue.push(t);})(arg[0], arg[1], arg[2]);};})(j[i]);}var n = document.createElement("script"),i = document.getElementsByTagName("script")[0]; n.type = "text/javascript", n.id = "sendinblue-js", n.async = !0, n.src = "https://sibautomation.com/sa.js?key=" + window.sib.client_key, i.parentNode.insertBefore(n, i), window.sendinblue.page();
})();
sendinblue.identify('#{current_administrateur.email}', {
'DS_SIGN_IN_COUNT' : '#{current_administrateur.sign_in_count}',
'DS_CREATED_AT' : '#{current_administrateur.created_at}',
'DS_ACTIVE' : '#{current_administrateur.active}',
'DS_ID' : '#{current_administrateur.id}'
});

View file

@ -18,19 +18,11 @@
= stylesheet_link_tag 'new_design/new_application', media: 'all', 'data-turbolinks-track': 'reload'
= stylesheet_link_tag 'new_design/print', media: 'print', 'data-turbolinks-track': 'reload'
= Gon::Base.render_data(camel_case: true, init: true)
- if Rails.env.development?
= stylesheet_link_tag :xray
= render partial: "layouts/matomo"
- if administrateur_signed_in?
= render partial: "layouts/sendinblue"
:javascript
DATA = [{
sentry: #{raw(sentry_config)}
}];
%body{ class: browser.platform.ios? ? 'ios' : nil }
.page-wrapper
= render partial: "layouts/outdated_browser_banner"
@ -50,3 +42,7 @@
= javascript_include_tag :xray
= yield :charts_js
= javascript_pack_tag 'track', async: true
- if administrateur_signed_in?
= javascript_pack_tag 'sendinblue', async: true

View file

@ -15,14 +15,7 @@
= javascript_pack_tag 'application-old', defer: true, 'data-turbolinks-track': 'reload'
= javascript_include_tag 'application', defer: true, 'data-turbolinks-track': 'reload'
= csrf_meta_tags
= render partial: "layouts/matomo"
= render partial: "layouts/sendinblue"
:javascript
DATA = [{
sentry: #{raw(sentry_config)}
}];
= Gon::Base.render_data(camel_case: true, init: true)
%body{ class: browser.platform.ios? ? 'ios' : nil }
= render partial: 'layouts/outdated_browser_banner'
@ -49,3 +42,6 @@
%i.fa.fa-times{ style: 'position: fixed; top: 10; right: 30; color: white;' }
= render partial: 'layouts/footer', locals: { main_container_size: main_container_size }
= javascript_pack_tag 'track', async: true
- if administrateur_signed_in?
= javascript_pack_tag 'sendinblue', async: true

View file

@ -41,6 +41,11 @@ SENTRY_ENABLED="disabled"
SENTRY_DSN_RAILS=""
SENTRY_DSN_JS=""
MATOMO_ENABLED="disabled"
MATOMO_ID="73"
SENDINBLUE_ENABLED="disabled"
MAILTRAP_ENABLED="disabled"
MAILTRAP_USERNAME=""
MAILTRAP_PASSWORD=""

View file

@ -52,7 +52,14 @@ defaults: &defaults
client_secret: <%= ENV['HELPSCOUT_CLIENT_SECRET'] %>
webhook_secret: <%= ENV['HELPSCOUT_WEBHOOK_SECRET'] %>
sendinblue:
enabled: <%= ENV['SENDINBLUE_ENABLED'] == 'enabled' %>
client_key: <%= ENV['SENDINBLUE_CLIENT_KEY'] %>
matomo:
enabled: <%= ENV['MATOMO_ENABLED'] == 'enabled' %>
client_key: <%= ENV['MATOMO_ID'] %>
sentry:
enabled: <%= ENV['SENTRY_ENABLED'] == 'enabled' %>
client_key: <%= ENV['SENTRY_DSN_JS'] %>
development:
<<: *defaults

View file

@ -1,7 +1,7 @@
{
"dependencies": {
"@rails/webpacker": "4.0.0-pre.3",
"@sentry/browser": "^4.0.4",
"@sentry/browser": "^4.6.5",
"@turf/area": "^6.0.1",
"activestorage": "^5.2.2-rc1",
"autocomplete.js": "^0.31.0",

106
yarn.lock
View file

@ -677,58 +677,57 @@
webpack-cli "^3.1.0"
webpack-sources "^1.2.0"
"@sentry/browser@^4.0.4":
version "4.0.4"
resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-4.0.4.tgz#6dfe05099d9ce23d7e9becaa905af5406520d448"
integrity sha512-qlrJICz5AqB+hz9p5DKC/Fl2pRnN6Rsl+cQQCraXQXroE3C+Z50bbIWEIwns11d//NpeRTOyHDNBqzXlNiy4YQ==
"@sentry/browser@^4.6.5":
version "4.6.5"
resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-4.6.5.tgz#ecae280400117fef039db1b678e110a7d3e98a10"
integrity sha512-sIbEDTdZeRN+jzCEHGBOdidjSv+ZmJ9VPfek+bnP5FZNyUYfaZRrwWG0sJPPb8SlhSPUQXSI1t1saRhvd+Gs4A==
dependencies:
"@sentry/core" "4.0.3"
"@sentry/types" "4.0.1"
"@sentry/utils" "4.0.1"
md5 "2.2.1"
"@sentry/core" "4.6.5"
"@sentry/types" "4.5.3"
"@sentry/utils" "4.6.5"
tslib "^1.9.3"
"@sentry/core@4.0.3":
version "4.0.3"
resolved "https://registry.yarnpkg.com/@sentry/core/-/core-4.0.3.tgz#4f8fd67888f1cf0f1a984c5fa362122b60e8bd08"
integrity sha512-vK6Ytk88STVk8rBxXnhOpo3ujlpPfQwiAFZDo+h7qJ0Xci5QxijqOHHZGEoBKF2ZhQAsohtt77DUIvOgiGr6Xw==
"@sentry/core@4.6.5":
version "4.6.5"
resolved "https://registry.yarnpkg.com/@sentry/core/-/core-4.6.5.tgz#70b4bcfc555f7eff80581a17baadac5500c2f75d"
integrity sha512-dT0FATtKAgd4dashwK+S2vYzCXIga3VJFJgkZVTK2aoy45E56ztxcbmNdI8O+3e67tGM5Il6CrD2fZg5yLty9A==
dependencies:
"@sentry/hub" "4.0.1"
"@sentry/minimal" "4.0.1"
"@sentry/types" "4.0.1"
"@sentry/utils" "4.0.1"
"@sentry/hub" "4.6.5"
"@sentry/minimal" "4.6.5"
"@sentry/types" "4.5.3"
"@sentry/utils" "4.6.5"
tslib "^1.9.3"
"@sentry/hub@4.0.1":
version "4.0.1"
resolved "https://registry.yarnpkg.com/@sentry/hub/-/hub-4.0.1.tgz#01870cede195029ae32d763199ff6c3e4edf99d1"
integrity sha512-XLpVrB8MJcquZpVstv7pXq1qjWH80mX7hln2CxWfeQTT5OmUMPGz0NbcaUyPA/QisvMYNo9eeVe2+lTnmSn5+Q==
"@sentry/hub@4.6.5":
version "4.6.5"
resolved "https://registry.yarnpkg.com/@sentry/hub/-/hub-4.6.5.tgz#451def7bc8d90d9cc007f58f364b3ce305c4701a"
integrity sha512-v9vee8s8C1fK/DPtNOzv6r+AMbPDOWfnasouNcBUkbQUSN5wUNyCDvt51QbWaw5kMMY5TSqjdVqY6gXQZI0APQ==
dependencies:
"@sentry/types" "4.0.0"
"@sentry/utils" "4.0.1"
"@sentry/types" "4.5.3"
"@sentry/utils" "4.6.5"
tslib "^1.9.3"
"@sentry/minimal@4.0.1":
version "4.0.1"
resolved "https://registry.yarnpkg.com/@sentry/minimal/-/minimal-4.0.1.tgz#c51a2af81eba48977fb54ab187e0c0eb0ad12c15"
integrity sha512-KKrgVATezmf/Wt1c0yH8csRwvV4s6TFGY1R3lgBwql3pKl3YYfL7RMqBke5elG9/3zWl6W4G9cleKZGAPLSvTA==
"@sentry/minimal@4.6.5":
version "4.6.5"
resolved "https://registry.yarnpkg.com/@sentry/minimal/-/minimal-4.6.5.tgz#64433d2c9fda69eedbb61855a7ff8905f7b19218"
integrity sha512-tf+J+uUNmSgzC7d9JSN8Ekw1HeBAX87Efa/jyFbzLvaw80oibvTiLSLqDjQ9PgvyIzBUuuPImkS2NpvPQGWFtg==
dependencies:
"@sentry/hub" "4.0.1"
"@sentry/types" "4.0.0"
"@sentry/hub" "4.6.5"
"@sentry/types" "4.5.3"
tslib "^1.9.3"
"@sentry/types@4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-4.0.0.tgz#9dd46a7b05004871fe0cea0b0423098d9d91a173"
integrity sha512-UaGfdu9DTcdQjyZ7nSQXAX4Nz1EzwDY6lFMfWxvb3hq7qDvr3bg0LOSFyjELbS6RcAkXwjB8BLpicABAXao3ag==
"@sentry/types@4.5.3":
version "4.5.3"
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-4.5.3.tgz#3350dce2b7f9b936a8c327891c12e3aef7bd8852"
integrity sha512-7ll1PAFNjrBNX9rzy3P2qAQrpQwHaDO3uKj735qsnGw34OtAS8Xr8WYrjI14f9fMPa/XIeWvMPb4GMic28V/ag==
"@sentry/types@4.0.1":
version "4.0.1"
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-4.0.1.tgz#f9342e905ce2aee71975574589d915b6fb691fb0"
integrity sha512-1Cq2gk/wZuBHB//HO830nykysHEsvZpjFcoIBHyqsJ7GjjcMxRAnO8ix0aw3hRfOsiPgD3mp8QomY9DqRHbjjA==
"@sentry/utils@4.0.1":
version "4.0.1"
resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-4.0.1.tgz#5690058fb030c23d46ea056aa3e8ebebb8105d45"
integrity sha512-SYbE1oe94TPnzcMlGZyCgIfo8e0NpkDo8sX1w43yHcE4HUbJ0NpK8z2FF4h52hShaog4ceLIXXSiYIKoKaJa2A==
"@sentry/utils@4.6.5":
version "4.6.5"
resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-4.6.5.tgz#4c960524914311eb76bbd6ca7f80f4d98c04db7f"
integrity sha512-rTISJtRRbWsd3UE+TkA3QG1C0VzPKPW8w74tieBwYhtTCGmOHNwz2nDC/MZGbGj4OgDmNKKl4CCyQr88EX08hA==
dependencies:
"@sentry/types" "4.0.0"
"@sentry/types" "4.5.3"
tslib "^1.9.3"
"@sindresorhus/is@^0.7.0":
version "0.7.0"
@ -1796,11 +1795,6 @@ chardet@^0.7.0:
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e"
integrity sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==
charenc@~0.0.1:
version "0.0.2"
resolved "https://registry.yarnpkg.com/charenc/-/charenc-0.0.2.tgz#c0a1d2f3a7092e03774bfa83f14c0fc5790a8667"
integrity sha1-wKHS86cJLgN3S/qD8UwPxXkKhmc=
chartkick@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/chartkick/-/chartkick-3.0.1.tgz#f325041bfc15f5d18074d4803549efc3751ccd81"
@ -2276,11 +2270,6 @@ cross-spawn@^6.0.0, cross-spawn@^6.0.5:
shebang-command "^1.2.0"
which "^1.2.9"
crypt@~0.0.1:
version "0.0.2"
resolved "https://registry.yarnpkg.com/crypt/-/crypt-0.0.2.tgz#88d7ff7ec0dfb86f713dc87bbb42d044d3e6c41b"
integrity sha1-iNf/fsDfuG9xPch7u0LQRNPmxBs=
crypto-browserify@^3.11.0:
version "3.12.0"
resolved "https://registry.yarnpkg.com/crypto-browserify/-/crypto-browserify-3.12.0.tgz#396cf9f3137f03e4b8e532c58f698254e00f80ec"
@ -4286,7 +4275,7 @@ is-binary-path@^1.0.0:
dependencies:
binary-extensions "^1.0.0"
is-buffer@^1.1.5, is-buffer@~1.1.1:
is-buffer@^1.1.5:
version "1.1.6"
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
integrity sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==
@ -5035,15 +5024,6 @@ md5.js@^1.3.4:
hash-base "^3.0.0"
inherits "^2.0.1"
md5@2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/md5/-/md5-2.2.1.tgz#53ab38d5fe3c8891ba465329ea23fac0540126f9"
integrity sha1-U6s41f48iJG6RlMp6iP6wFQBJvk=
dependencies:
charenc "~0.0.1"
crypt "~0.0.1"
is-buffer "~1.1.1"
mdn-data@~1.1.0:
version "1.1.4"
resolved "https://registry.yarnpkg.com/mdn-data/-/mdn-data-1.1.4.tgz#50b5d4ffc4575276573c4eedb8780812a8419f01"
@ -8158,7 +8138,7 @@ trim-right@^1.0.1:
dependencies:
glob "^6.0.4"
tslib@^1.9.0:
tslib@^1.9.0, tslib@^1.9.3:
version "1.9.3"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.3.tgz#d7e4dd79245d85428c4d7e4822a79917954ca286"
integrity sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ==