Skip to content

Commit

Permalink
Rename analytics script to readers.min.js. (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
rla committed Dec 14, 2018
1 parent a765143 commit 3be8810
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 19 deletions.
16 changes: 8 additions & 8 deletions analytics/visitor.js → analytics/readers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

// Reads the previously stored user id.
const readUserId = () => {
const match = document.cookie.match(/visitor_user\s*=\s*([a-z0-9\-]+)/);
const match = document.cookie.match(/readers_user\s*=\s*([a-z0-9\-]+)/);
return match ? match[1] : null;
};

// User ids use permanent cookies.
const writeUserId = (userId) => {
const expires = new Date();
expires.setFullYear(expires.getFullYear() + 1);
document.cookie = 'visitor_user=' + userId + '; path=/; expires=' + expires.toUTCString();
document.cookie = 'readers_user=' + userId + '; path=/; expires=' + expires.toUTCString();
};

const readSessionId = () => {
const match = document.cookie.match(/visitor_session\s*=\s*([a-z0-9\-]+)/);
const match = document.cookie.match(/readers_session\s*=\s*([a-z0-9\-]+)/);
return match ? match[1] : null;
};

const writeSessionId = (sessionId) => {
document.cookie = 'visitor_session=' + sessionId + '; path=/';
document.cookie = 'readers_session=' + sessionId + '; path=/';
};

// Helper to run HTTP POST request to the backend.
Expand Down Expand Up @@ -50,7 +50,7 @@
cb(userId);
} else {
// Record the new user.
postJSON('/api/visitor/user', {}, (response) => {
postJSON('/api/readers/user', {}, (response) => {
if (response.status === 'success') {
const userId = response.data;
// Remember for later.
Expand All @@ -76,7 +76,7 @@
agent: navigator.userAgent || null,
platform: navigator.platform || null
};
postJSON('/api/visitor/session', data, (response) => {
postJSON('/api/readers/session', data, (response) => {
if (response.status === 'success') {
const sessionId = response.data;
// Remember for later.
Expand All @@ -97,7 +97,7 @@
pageview_id: pageviewId,
elapsed: elapsed
}
postJSON('/api/visitor/pageview_extend', data, (response) => {
postJSON('/api/readers/pageview_extend', data, (response) => {
if (response.status === 'success') {
cb(userId, sessionId);
}
Expand All @@ -110,7 +110,7 @@
elapsed: elapsed
};
// First pageview record.
postJSON('/api/visitor/pageview', data, (response) => {
postJSON('/api/readers/pageview', data, (response) => {
if (response.status === 'success') {
// Update the current pageview id.
pageviewId = response.data;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"webpack-cli": "^3.1.2"
},
"scripts": {
"build-analytics": "webpack --mode production --config webpack.js",
"build-readers": "webpack --mode production --config webpack.readers.js",
"build-admin": "webpack --mode production --config webpack.admin.js",
"build-admin-watch": "webpack --watch --mode production --config webpack.admin.js"
}
Expand Down
19 changes: 11 additions & 8 deletions prolog/bc/bc_api_analytics.pl
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,52 @@
:- use_module(bc_analytics).
:- use_module(bc_analytics_db).

:- route_post(api/visitor/user, record_user).
% Handlers for data from the readers script.

:- route_post(api/readers/user, record_user).

record_user:-
bc_read_by_schema(bc_analytics_user, User),
bc_record_user(User, UserId),
bc_reply_success(UserId).

:- route_post(api/visitor/session, record_session).
:- route_post(api/readers/session, record_session).

record_session:-
bc_read_by_schema(bc_analytics_session, Session),
bc_record_session(Session, SessionId),
bc_reply_success(SessionId).

:- route_post(api/visitor/pageview, record_pageview).
:- route_post(api/readers/pageview, record_pageview).

record_pageview:-
bc_read_by_schema(bc_analytics_pageview, Pageview),
bc_record_pageview(Pageview, PageviewId),
bc_reply_success(PageviewId).

:- route_post(api/visitor/pageview_extend, record_pageview_extend).
:- route_post(api/readers/pageview_extend, record_pageview_extend).

record_pageview_extend:-
bc_read_by_schema(bc_analytics_pageview_extend, Pageview),
bc_record_pageview_extend(Pageview),
bc_reply_success(true).

:- route_get(bc/'visitor.min.js', visitor_script).
:- route_get(bc/'readers.min.js', visitor_script).

visitor_script:-
http_current_request(Request),
module_property(bc_api_analytics, file(File)),
file_directory_name(File, Dir),
atom_concat(Dir, '/public/js/visitor.min.js', Path),
atom_concat(Dir, '/public/js/readers.min.js', Path),
http_reply_file(Path, [unsafe(true)], Request).

:- route_get(bc/'visitor.min.js.map', visitor_script_map).
:- route_get(bc/'readers.min.js.map', visitor_script_map).

visitor_script_map:-
http_current_request(Request),
module_property(bc_api_analytics, file(File)),
file_directory_name(File, Dir),
atom_concat(Dir, '/public/js/visitor.min.js.map', Path),
atom_concat(Dir, '/public/js/readers.min.js.map', Path),
http_reply_file(Path, [unsafe(true)], Request).

:- register_schema(bc_analytics_user, _{
Expand Down Expand Up @@ -100,6 +102,7 @@

% Analytic timeseries results for the administration API.

% TODO: add auth
:- route_get(api/analytics/timeseries/From/To/Duration,
analytics_timeseries(From, To, Duration)).

Expand Down
2 changes: 2 additions & 0 deletions prolog/bc/public/js/readers.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions prolog/bc/public/js/readers.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions webpack.js → webpack.readers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const assert = require('assert');
// Returns configuration for the given bundle.

module.exports = {
entry: path.join(__dirname, 'analytics', 'visitor.js'),
entry: path.join(__dirname, 'analytics', 'readers.js'),
output: {
path: path.resolve(__dirname, 'prolog', 'bc', 'public', 'js'),
filename: `visitor.min.js`
filename: `readers.min.js`
},
devtool: 'source-map',
module: {
Expand Down

0 comments on commit 3be8810

Please sign in to comment.