Disabled attribution calculation when tracking is disabled (#15710)

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

- forces attribution service to use empty history or context if attribution tracking is disabled
This commit is contained in:
Rishabh Garg 2022-10-27 21:10:03 +05:30 committed by GitHub
parent 5cb3c43e80
commit f1aff45dc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 12 deletions

View File

@ -1,5 +1,6 @@
const urlService = require('../url');
const urlUtils = require('../../../shared/url-utils');
const settingsCache = require('../../../shared/settings-cache');
class MemberAttributionServiceWrapper {
init() {
@ -38,7 +39,8 @@ class MemberAttributionServiceWrapper {
SubscriptionCreatedEvent: models.SubscriptionCreatedEvent,
Integration: models.Integration
},
attributionBuilder: this.attributionBuilder
attributionBuilder: this.attributionBuilder,
isTrackingEnabled: !!settingsCache.get('members_track_sources')
});
}
}

View File

@ -55,6 +55,14 @@
"members_stripe_webhook_secret": {
"defaultValue": null,
"type": "string"
},
"members_track_sources": {
"defaultValue": "true",
"validations": {
"isEmpty": false,
"isIn": [["true", "false"]]
},
"type": "boolean"
}
},
"site": {

View File

@ -5,13 +5,15 @@ class MemberAttributionService {
*
* @param {Object} deps
* @param {Object} deps.attributionBuilder
* @param {boolean} deps.isTrackingEnabled
* @param {Object} deps.models
* @param {Object} deps.models.MemberCreatedEvent
* @param {Object} deps.models.SubscriptionCreatedEvent
*/
constructor({attributionBuilder, models}) {
constructor({attributionBuilder, models, isTrackingEnabled}) {
this.models = models;
this.attributionBuilder = attributionBuilder;
this.isTrackingEnabled = isTrackingEnabled;
}
/**
@ -20,7 +22,7 @@ class MemberAttributionService {
* @returns {Promise<import('./attribution').AttributionResource|null>}
*/
async getAttributionFromContext(context) {
if (!context) {
if (!context || !this.isTrackingEnabled) {
return null;
}
@ -68,7 +70,10 @@ class MemberAttributionService {
* @returns {Promise<import('./attribution').Attribution>}
*/
async getAttribution(historyArray) {
const history = UrlHistory.create(historyArray);
let history = UrlHistory.create(historyArray);
if (!this.isTrackingEnabled) {
history = UrlHistory.create([]);
}
return await this.attributionBuilder.getAttribution(history);
}

View File

@ -12,28 +12,45 @@ describe('MemberAttributionService', function () {
describe('getAttributionFromContext', function () {
it('returns null if no context is provided', async function () {
const service = new MemberAttributionService({});
const service = new MemberAttributionService({
isTrackingEnabled: true
});
const attribution = await service.getAttributionFromContext();
should(attribution).be.null();
});
it('returns null if tracking is disabled is provided', async function () {
const service = new MemberAttributionService({
isTrackingEnabled: false
});
const attribution = await service.getAttributionFromContext();
should(attribution).be.null();
});
it('returns attribution for importer context', async function () {
const service = new MemberAttributionService({});
const service = new MemberAttributionService({
isTrackingEnabled: true
});
const attribution = await service.getAttributionFromContext({importer: true});
should(attribution).containEql({referrerSource: 'Imported', referrerMedium: 'Member Importer'});
});
it('returns attribution for admin context', async function () {
const service = new MemberAttributionService({});
const service = new MemberAttributionService({
isTrackingEnabled: true
});
const attribution = await service.getAttributionFromContext({user: 'abc'});
should(attribution).containEql({referrerSource: 'Created manually', referrerMedium: 'Ghost Admin'});
});
it('returns attribution for api without integration context', async function () {
const service = new MemberAttributionService({});
const service = new MemberAttributionService({
isTrackingEnabled: true
});
const attribution = await service.getAttributionFromContext({
api_key: 'abc'
});
@ -51,7 +68,8 @@ describe('MemberAttributionService', function () {
};
}
}
}
},
isTrackingEnabled: true
});
const attribution = await service.getAttributionFromContext({
api_key: 'abc',
@ -77,7 +95,8 @@ describe('MemberAttributionService', function () {
}
};
}
}
},
isTrackingEnabled: true
});
const model = {
id: 'event_id',
@ -110,7 +129,8 @@ describe('MemberAttributionService', function () {
}
};
}
}
},
isTrackingEnabled: true
});
const model = {
id: 'event_id',
@ -149,7 +169,8 @@ describe('MemberAttributionService', function () {
}
};
}
}
},
isTrackingEnabled: true
});
const model = {
id: 'event_id',