Skip to content

Commit

Permalink
fix: ensure plausible always gets data
Browse files Browse the repository at this point in the history
  • Loading branch information
sznowicki committed Dec 17, 2023
1 parent f000b18 commit 1365b78
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
3 changes: 2 additions & 1 deletion functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export const onRequestGet = async (context) => {

const hasResults = results.length > 0;

emitPageView(context, {
// without await it might get killed before sending by cloudflare
await emitPageView(context, {
hasResults,
});

Expand Down
2 changes: 2 additions & 0 deletions lib/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const callMongo = async (envs, action, data) => {
body: JSON.stringify(data)
});

console.log(request);

const response = await request.json();

return response;
Expand Down
14 changes: 8 additions & 6 deletions lib/plausible.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ const emitEvent = async (context, name, props = {}) => {
};
/**
* Emits PageView to Plausible.io
* Note: this is filly GDPR/Telecom compliant as we anonymize the IP address.
* Note: this is fully GDPR/Telecom compliant as we anonymize the IP address.
* Should never throw. Should be fire and forget.
* @param context
* @param {Object} props
*/
export const emitPageView = (context, props) => {
emitEvent(context, 'pageview', props)
.catch((error) => {
console.error('Plausible failed', error);
});
export const emitPageView = async (context, props) => {
try {
await emitEvent(context, 'pageview', props);
} catch (error) {
console.error('Plausible failed', error);
}
};

0 comments on commit 1365b78

Please sign in to comment.