-
Notifications
You must be signed in to change notification settings - Fork 0
/
LastNonDirectInteraction.ts
26 lines (22 loc) · 1.2 KB
/
LastNonDirectInteraction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { AttributionModel, Interaction } from '../types';
/**
* This implements the "last non-direct interaction" attribution model
* which returns the last non-direct interaction
* as direct interactions may be logged after non-direct interactions once the specified session-timeout has passed
*
* Since only one interaction is returned, it is not weighted
*/
const lastNonDirectInteraction: AttributionModel = (interactions: Interaction[]): Interaction => {
if (interactions.length === 0) {
return null;
}
const nonExcludedInteractions = interactions.filter((interaction) => !interaction.excluded);
const nonExcludedNonDirectInteractions = nonExcludedInteractions.filter((interaction) => !interaction.direct);
// First, we attempt to return the last non-excluded non-direct interaction
// Then if all we had were excluded and/or direct interactions we attempt to return the last non-excluded direct interaction
// Then if all we had were excluded interactions we return the last one as it's better than nothing
return nonExcludedNonDirectInteractions.pop()
|| nonExcludedInteractions.pop()
|| interactions.pop();
}
export default lastNonDirectInteraction;