Skip to content

Commit

Permalink
Log interaction before notifying subscribers (#8)
Browse files Browse the repository at this point in the history
This way subscribers can get the full interaction log, including the interaction that just changed attribution.
  • Loading branch information
JeroenBakker authored Aug 25, 2023
1 parent 47644a6 commit cb47625
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.4.1] - 2023-08-25

### Fixed

- Subscribers are now notified of changed attribution after logging the new interaction.
This way the subscribers have access to the full interaction log, including the interaction that just changed attribution.

## [0.4.0] - 2023-08-23

### Added
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jeroen.bakker/just-attribute",
"version": "0.4.0",
"version": "0.4.1",
"description": "Realtime privacy-conscious marketing attribution for the web",
"author": "Jeroen Bakker",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions src/InteractionLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ export default class InteractionLogger {
}

if (this.hasAttributionChanged(interaction, lastInteractionTimestamp)) {
this.logInteraction(interaction);

// Notify all subscribers that the attribution has changed and pass along the latest attribution
this.attributionChangeCallbacks.forEach((callback) => callback(interaction));

this.logInteraction(interaction);
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/InteractionLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ test('it calls callback on changed attribution', async () => {
expect(callback).toHaveBeenCalledTimes(2);
});

// By logging the interaction first, any subscribers can use the interaction log including the new interaction
test('it logs interaction before notifying subscribers', () => {
const logger = new InteractionLogger();

const assertLogHasOneEntry = () => {
expect(logger.interactionLog().length).toBe(1);
};

logger.onAttributionChange(assertLogHasOneEntry);
logger.pageview(new URL('https://example.com/'), false);
});

test('it clears the log', async () => {
const logger = new InteractionLogger();
logger.pageview(new URL('https://example.com/'), false);
Expand Down

0 comments on commit cb47625

Please sign in to comment.