Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tracker): send unload event on page hidden instead #139

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions tracker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ The minified gzipped tracker is less than 1KB. The size is measured in its compr

Our tracker is designed with compression in mind, given that web traffic is usually compressed. For example, certain optimisation techniques like inlining globals with shorter variable names are avoided, as they may decrease the uncompressed size of the tracker but result in an increase in the compressed size due to how dictionary-based compression techniques work.

| File | Size | Compressed (gzip) | Compressed (brotli) |
| --------------------- | -------------------- | -------------------- | ------------------- |
| `default.min.js` | 1574 bytes (1.54 KB) | 792 bytes (0.77 KB) | 639 bytes (0.62 KB) |
| `page-events.min.js` | 1812 bytes (1.77 KB) | 916 bytes (0.89 KB) | 751 bytes (0.73 KB) |
| `click-events.min.js` | 2053 bytes (2.00 KB) | 1003 bytes (0.98 KB) | 810 bytes (0.79 KB) |
| File | Size | Compressed (gzip) | Compressed (brotli) |
| --------------------- | -------------------- | ------------------- | ------------------- |
| `default.min.js` | 1512 bytes (1.48 KB) | 766 bytes (0.75 KB) | 620 bytes (0.61 KB) |
| `page-events.min.js` | 1750 bytes (1.71 KB) | 890 bytes (0.87 KB) | 731 bytes (0.71 KB) |
| `click-events.min.js` | 1991 bytes (1.94 KB) | 977 bytes (0.95 KB) | 792 bytes (0.77 KB) |

The listed sizes only show the size of the tracker itself with one specific feature. When combining multiple features, the size of the tracker will relatively increase (although some features may share code with each other).

Expand Down
22 changes: 6 additions & 16 deletions tracker/dist/click-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,11 @@
*/
let isUnique = true;

/**
* A temporary variable to store the start time of the page when it is hidden.
*/
let hiddenStartTime = 0;

/**
* The total time the user has had the page hidden.
* It also signifies the start epoch time of the page.
*/
let hiddenTotalTime = Date.now();
let startTime = Date.now();

/**
* Ensure only the unload beacon is called once.
Expand Down Expand Up @@ -117,8 +112,7 @@
// However, isFirstVisit will be called on each page load, so we don't need to reset it.
isUnique = false;
uid = generateUid();
hiddenStartTime = 0;
hiddenTotalTime = Date.now();
startTime = Date.now();
isUnloadCalled = false;
};

Expand Down Expand Up @@ -259,7 +253,7 @@
({
"b": uid,
"e": "unload",
"m": Date.now() - hiddenTotalTime,
"m": Date.now() - startTime,
}),
),
);
Expand Down Expand Up @@ -331,19 +325,15 @@
addEventListener(
'visibilitychange',
() => {
if (document.visibilityState == 'hidden') {
if (document.hidden) {
// Page is hidden, record the current time.
hiddenStartTime = Date.now();
} else {
// Page is visible, subtract the hidden time to calculate the total time hidden.
hiddenTotalTime += Date.now() - hiddenStartTime;
hiddenStartTime = 0;
sendUnloadBeacon();
}
},
{ capture: true },
);

pingCache(host + 'event/ping?root').then((response) => {
pingCache(host + 'event/ping').then((response) => {
// The response is a boolean indicating if the user is unique or not.
isUnique = response;

Expand Down
2 changes: 1 addition & 1 deletion tracker/dist/click-events.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 6 additions & 16 deletions tracker/dist/click-events.page-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,11 @@
*/
let isUnique = true;

/**
* A temporary variable to store the start time of the page when it is hidden.
*/
let hiddenStartTime = 0;

/**
* The total time the user has had the page hidden.
* It also signifies the start epoch time of the page.
*/
let hiddenTotalTime = Date.now();
let startTime = Date.now();

/**
* Ensure only the unload beacon is called once.
Expand Down Expand Up @@ -117,8 +112,7 @@
// However, isFirstVisit will be called on each page load, so we don't need to reset it.
isUnique = false;
uid = generateUid();
hiddenStartTime = 0;
hiddenTotalTime = Date.now();
startTime = Date.now();
isUnloadCalled = false;
};

Expand Down Expand Up @@ -267,7 +261,7 @@
({
"b": uid,
"e": "unload",
"m": Date.now() - hiddenTotalTime,
"m": Date.now() - startTime,
}),
),
);
Expand Down Expand Up @@ -339,19 +333,15 @@
addEventListener(
'visibilitychange',
() => {
if (document.visibilityState == 'hidden') {
if (document.hidden) {
// Page is hidden, record the current time.
hiddenStartTime = Date.now();
} else {
// Page is visible, subtract the hidden time to calculate the total time hidden.
hiddenTotalTime += Date.now() - hiddenStartTime;
hiddenStartTime = 0;
sendUnloadBeacon();
}
},
{ capture: true },
);

pingCache(host + 'event/ping?root').then((response) => {
pingCache(host + 'event/ping').then((response) => {
// The response is a boolean indicating if the user is unique or not.
isUnique = response;

Expand Down
2 changes: 1 addition & 1 deletion tracker/dist/click-events.page-events.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 6 additions & 16 deletions tracker/dist/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,11 @@
*/
let isUnique = true;

/**
* A temporary variable to store the start time of the page when it is hidden.
*/
let hiddenStartTime = 0;

/**
* The total time the user has had the page hidden.
* It also signifies the start epoch time of the page.
*/
let hiddenTotalTime = Date.now();
let startTime = Date.now();

/**
* Ensure only the unload beacon is called once.
Expand Down Expand Up @@ -117,8 +112,7 @@
// However, isFirstVisit will be called on each page load, so we don't need to reset it.
isUnique = false;
uid = generateUid();
hiddenStartTime = 0;
hiddenTotalTime = Date.now();
startTime = Date.now();
isUnloadCalled = false;
};

Expand Down Expand Up @@ -243,7 +237,7 @@
({
"b": uid,
"e": "unload",
"m": Date.now() - hiddenTotalTime,
"m": Date.now() - startTime,
}),
),
);
Expand Down Expand Up @@ -273,19 +267,15 @@
addEventListener(
'visibilitychange',
() => {
if (document.visibilityState == 'hidden') {
if (document.hidden) {
// Page is hidden, record the current time.
hiddenStartTime = Date.now();
} else {
// Page is visible, subtract the hidden time to calculate the total time hidden.
hiddenTotalTime += Date.now() - hiddenStartTime;
hiddenStartTime = 0;
sendUnloadBeacon();
}
},
{ capture: true },
);

pingCache(host + 'event/ping?root').then((response) => {
pingCache(host + 'event/ping').then((response) => {
// The response is a boolean indicating if the user is unique or not.
isUnique = response;

Expand Down
2 changes: 1 addition & 1 deletion tracker/dist/default.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 6 additions & 16 deletions tracker/dist/page-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,11 @@
*/
let isUnique = true;

/**
* A temporary variable to store the start time of the page when it is hidden.
*/
let hiddenStartTime = 0;

/**
* The total time the user has had the page hidden.
* It also signifies the start epoch time of the page.
*/
let hiddenTotalTime = Date.now();
let startTime = Date.now();

/**
* Ensure only the unload beacon is called once.
Expand Down Expand Up @@ -117,8 +112,7 @@
// However, isFirstVisit will be called on each page load, so we don't need to reset it.
isUnique = false;
uid = generateUid();
hiddenStartTime = 0;
hiddenTotalTime = Date.now();
startTime = Date.now();
isUnloadCalled = false;
};

Expand Down Expand Up @@ -267,7 +261,7 @@
({
"b": uid,
"e": "unload",
"m": Date.now() - hiddenTotalTime,
"m": Date.now() - startTime,
}),
),
);
Expand Down Expand Up @@ -297,19 +291,15 @@
addEventListener(
'visibilitychange',
() => {
if (document.visibilityState == 'hidden') {
if (document.hidden) {
// Page is hidden, record the current time.
hiddenStartTime = Date.now();
} else {
// Page is visible, subtract the hidden time to calculate the total time hidden.
hiddenTotalTime += Date.now() - hiddenStartTime;
hiddenStartTime = 0;
sendUnloadBeacon();
}
},
{ capture: true },
);

pingCache(host + 'event/ping?root').then((response) => {
pingCache(host + 'event/ping').then((response) => {
// The response is a boolean indicating if the user is unique or not.
isUnique = response;

Expand Down
Loading
Loading