From 7f0208ed8fb423ccb492313f55057490935d3cae Mon Sep 17 00:00:00 2001 From: tfloxolodeiro Date: Mon, 5 Apr 2021 19:06:44 -0300 Subject: [PATCH 1/2] using lastInteraction --- app/services/pilas-bloques-analytics.js | 12 ++++++-- app/services/storage.js | 16 +++++++---- .../services/pilas-bloques-analytics-test.js | 28 +++++++++++-------- tests/unit/services/pilas-bloques-api-test.js | 2 +- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/app/services/pilas-bloques-analytics.js b/app/services/pilas-bloques-analytics.js index 122314c0d..d05c69102 100644 --- a/app/services/pilas-bloques-analytics.js +++ b/app/services/pilas-bloques-analytics.js @@ -31,22 +31,28 @@ export default Service.extend({ _newSession() { const newSession = { id: uuidv4(), - firstInteraction: new Date(), + lastInteraction: new Date(), answers: [] } this.storage.saveAnalyticsSession(newSession) return newSession }, + _updatedSession(session) { + const updatedSession = { ...session, lastInteraction: new Date() } + this.storage.saveAnalyticsSession(updatedSession) + return updatedSession + }, + getSession() { const session = this.storage.getAnalyticsSession() if (!session) return this._newSession() - return this._isOld(session) ? this._newSession() : session + return this._isOld(session) ? this._newSession() : this._updatedSession(session) }, logout() { this.storage.saveAnalyticsSession(null) }, - _isOld({ firstInteraction }) { return (new Date() - new Date(firstInteraction)) / 1000 / 60 > sessionExpire }, // Minutes + _isOld({ lastInteraction }) { return (new Date() - lastInteraction) / 1000 / 60 > sessionExpire }, // Minutes }) diff --git a/app/services/storage.js b/app/services/storage.js index 8a4c2d748..c5db39ab1 100644 --- a/app/services/storage.js +++ b/app/services/storage.js @@ -4,18 +4,22 @@ export default Ember.Service.extend({ USER_KEY: 'PB_USER', ANALYTICS_KEY: 'PB_ANALYTICS_SESSION', - getUserId() { + getUserId() { const user = this.getUser() return user && user.id }, - getUser() { return this._get(this.USER_KEY) }, - saveUser(user) { this._save(this.USER_KEY, user) }, + getUser() { return this._get(this.USER_KEY) }, + saveUser(user) { this._save(this.USER_KEY, user) }, + + getAnalyticsSession() { + const session = this._get(this.ANALYTICS_KEY) + return session && { ...session, lastInteraction: new Date(session.lastInteraction) } + }, - getAnalyticsSession() { return this._get(this.ANALYTICS_KEY) }, saveAnalyticsSession(session) { this._save(this.ANALYTICS_KEY, session) }, - _get(key) { return JSON.parse(localStorage.getItem(key) || null) }, - _save(key, data = null) { localStorage.setItem(key, JSON.stringify(data)) }, + _get(key) { return JSON.parse(localStorage.getItem(key) || null) }, + _save(key, data = null) { localStorage.setItem(key, JSON.stringify(data)) }, }) \ No newline at end of file diff --git a/tests/unit/services/pilas-bloques-analytics-test.js b/tests/unit/services/pilas-bloques-analytics-test.js index 2256c17f8..ca68de867 100644 --- a/tests/unit/services/pilas-bloques-analytics-test.js +++ b/tests/unit/services/pilas-bloques-analytics-test.js @@ -17,41 +17,47 @@ module('Unit | Service | pilas-bloques-analytics', function (hooks) { test('Should save session', function (assert) { pbAnalytics.getSession() - const { id, firstInteraction, answers } = getSession() + const { id, lastInteraction, answers } = getStorageSession() assert.ok(id) - assert.ok(firstInteraction) + assert.ok(lastInteraction) assert.ok(answers) }) test('Should add new answers', function (assert) { pbAnalytics.newAnswer({ id: 'TEST' }) - const { answers } = getSession() + const { answers } = getStorageSession() assert.deepEqual(answers, [{ id: 'TEST' }]) }) test('Should keep the session for a while', function (assert) { const firstSessionId = pbAnalytics.getSession().id - changeSessionFirstInteractionByMinutes(1) + substractMinutesFromStorageSession(1) const currentSessionId = pbAnalytics.getSession().id assert.equal(firstSessionId, currentSessionId) }) test('Should change the session after a long time', function (assert) { const firstSessionId = pbAnalytics.getSession().id - changeSessionFirstInteractionByMinutes(31) + substractMinutesFromStorageSession(31) const currentSessionId = pbAnalytics.getSession().id assert.notEqual(firstSessionId, currentSessionId) }) - function getSession() { + test('Should update lastInteraction after an interaction', function (assert) { + pbAnalytics.getSession() // Creates session + substractMinutesFromStorageSession(15) + const firstSessionInteraction = getStorageSession().lastInteraction + + assert.ok(pbAnalytics.getSession().lastInteraction > firstSessionInteraction) + }) + + function getStorageSession() { return storage.getAnalyticsSession() } - function changeSessionFirstInteractionByMinutes(minutes) { - const session = getSession() - const before = new Date() - before.setMinutes(before.getMinutes() - minutes) - session.firstInteraction = before + function substractMinutesFromStorageSession(minutes) { + const session = getStorageSession() + session.lastInteraction.setMinutes(session.lastInteraction.getMinutes() - minutes) storage.saveAnalyticsSession(session) } diff --git a/tests/unit/services/pilas-bloques-api-test.js b/tests/unit/services/pilas-bloques-api-test.js index 9785392a3..abff19ce3 100644 --- a/tests/unit/services/pilas-bloques-api-test.js +++ b/tests/unit/services/pilas-bloques-api-test.js @@ -69,7 +69,7 @@ module('Unit | Service | pilas-bloques-api', function (hooks) { test('should add context to body', async function (assert) { await api.login({}) const context = fetchCallBody().context - assertHasProps(assert, context, 'answers', 'browserId', 'firstInteraction', 'id', 'online', 'userId') + assertHasProps(assert, context, 'answers', 'browserId', 'lastInteraction', 'id', 'online', 'userId') }) test('should add timestamp to body', async function (assert) { From 473872e9d5cf60e31a4bcc445e536804cc7f8e2b Mon Sep 17 00:00:00 2001 From: tfloxolodeiro Date: Wed, 7 Apr 2021 13:12:54 -0300 Subject: [PATCH 2/2] getSession in beforeEach in test --- tests/unit/services/pilas-bloques-analytics-test.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/unit/services/pilas-bloques-analytics-test.js b/tests/unit/services/pilas-bloques-analytics-test.js index ca68de867..9d839ef3a 100644 --- a/tests/unit/services/pilas-bloques-analytics-test.js +++ b/tests/unit/services/pilas-bloques-analytics-test.js @@ -6,17 +6,18 @@ module('Unit | Service | pilas-bloques-analytics', function (hooks) { var pbAnalytics var storage + var firstSessionId hooks.beforeEach(function () { pbAnalytics = this.owner.lookup('service:pilas-bloques-analytics') storage = this.owner.lookup('service:storage') + firstSessionId = pbAnalytics.getSession().id // Creates session }) test('Should create new sessionId', function (assert) { - assert.ok(pbAnalytics.getSession().id) + assert.ok(firstSessionId) }) test('Should save session', function (assert) { - pbAnalytics.getSession() const { id, lastInteraction, answers } = getStorageSession() assert.ok(id) assert.ok(lastInteraction) @@ -30,21 +31,18 @@ module('Unit | Service | pilas-bloques-analytics', function (hooks) { }) test('Should keep the session for a while', function (assert) { - const firstSessionId = pbAnalytics.getSession().id substractMinutesFromStorageSession(1) const currentSessionId = pbAnalytics.getSession().id assert.equal(firstSessionId, currentSessionId) }) test('Should change the session after a long time', function (assert) { - const firstSessionId = pbAnalytics.getSession().id substractMinutesFromStorageSession(31) const currentSessionId = pbAnalytics.getSession().id assert.notEqual(firstSessionId, currentSessionId) }) test('Should update lastInteraction after an interaction', function (assert) { - pbAnalytics.getSession() // Creates session substractMinutesFromStorageSession(15) const firstSessionInteraction = getStorageSession().lastInteraction