Added the last_seen_at update on email open

refs https://github.com/TryGhost/Team/issues/1306

- Updates once every day (in the publication timezone)
- Also updates when the value is `NULL`
- This is implemented as a SQL query as the code is on the job and doesn't have visibility to the members code
This commit is contained in:
Thibaut Patel 2022-02-22 19:43:11 +01:00 committed by Thibaut Patel
parent bf45ef4a87
commit 73a049c942
3 changed files with 28 additions and 2 deletions

View File

@ -1,5 +1,5 @@
const {EventProcessor} = require('@tryghost/email-analytics-service');
const moment = require('moment');
const moment = require('moment-timezone');
class GhostEventProcessor extends EventProcessor {
constructor({db}) {
@ -88,6 +88,17 @@ class GhostEventProcessor extends EventProcessor {
opened_at: this.db.knex.raw('COALESCE(opened_at, ?)', [moment.utc(event.timestamp).format('YYYY-MM-DD HH:mm:ss')])
});
const {value: timezone} = await this.db.knex('settings').first('value').where('key', 'timezone');
await this.db.knex('members')
.where('email', '=', event.recipientEmail)
.andWhere(builder => builder
.where('last_seen_at', '<', moment.utc(event.timestamp).tz(timezone).startOf('day').format('YYYY-MM-DD HH:mm:ss'))
.orWhereNull('last_seen_at')
)
.update({
last_seen_at: moment.utc(event.timestamp).format('YYYY-MM-DD HH:mm:ss')
});
return updateResult > 0;
}

View File

@ -82,6 +82,7 @@
"@tryghost/logging": "2.0.4",
"@tryghost/magic-link": "1.0.19",
"@tryghost/members-api": "5.0.4",
"@tryghost/members-events-service": "0.1.2",
"@tryghost/members-importer": "0.5.2",
"@tryghost/members-offers": "0.10.7",
"@tryghost/members-ssr": "1.0.22",

View File

@ -2190,6 +2190,11 @@
"@tryghost/tpl" "^0.1.4"
bson-objectid "^2.0.1"
"@tryghost/member-events@0.4.0":
version "0.4.0"
resolved "https://registry.yarnpkg.com/@tryghost/member-events/-/member-events-0.4.0.tgz#6bef156f477cefa780b9c1651c41a1503ace4247"
integrity sha512-i4URWIo6gV4zUCa9rCMYBYK3L8WGt1jONa4uGEEqlXHOf0YW40rLhdqlDhIzF/T5hSsLDZhFwkUx43+sGSOPwA==
"@tryghost/member-events@^0.3.5":
version "0.3.5"
resolved "https://registry.yarnpkg.com/@tryghost/member-events/-/member-events-0.3.5.tgz#f51e4e86b39792cfc9a1be0f7644da480ae274dc"
@ -2247,6 +2252,15 @@
papaparse "5.3.1"
pump "^3.0.0"
"@tryghost/members-events-service@0.1.2":
version "0.1.2"
resolved "https://registry.yarnpkg.com/@tryghost/members-events-service/-/members-events-service-0.1.2.tgz#e8bc9dac1eca98f0cbe0372bc2460573b24f21bf"
integrity sha512-VbMAejI6daUiTyQCsA3FOhno7bPQNMWugbihAObwfyQayjoas8hYfrjk9/z9QMYh4N0A5JQugvXkL1gDQpbeLA==
dependencies:
"@tryghost/domain-events" "0.1.8"
"@tryghost/member-events" "0.4.0"
moment-timezone "0.5.34"
"@tryghost/members-importer@0.5.2":
version "0.5.2"
resolved "https://registry.yarnpkg.com/@tryghost/members-importer/-/members-importer-0.5.2.tgz#da71da7172431b31d1b05fc2eada494b1b8d3cc3"
@ -9138,7 +9152,7 @@ mock-knex@0.4.10:
lodash "^4.14.2"
semver "^5.3.0"
moment-timezone@0.5.23, moment-timezone@^0.5.31, moment-timezone@^0.5.33:
moment-timezone@0.5.23, moment-timezone@0.5.34, moment-timezone@^0.5.31, moment-timezone@^0.5.33:
version "0.5.23"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.23.tgz#7cbb00db2c14c71b19303cb47b0fb0a6d8651463"
integrity sha512-WHFH85DkCfiNMDX5D3X7hpNH3/PUhjTGcD0U1SgfBGZxJ3qUmJh5FdvaFjcClxOvB3rzdfj4oRffbI38jEnC1w==