-
Notifications
You must be signed in to change notification settings - Fork 29
Add plausible analytics #176
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f634089
Add plausible analytics
Ahmedhossamdev 5515de3
Add analytics configuration options to .env.example and update README
Ahmedhossamdev 5bc40cc
Update README
Ahmedhossamdev bbb1e69
Remove commented-out PlausibleAnalytics component
Ahmedhossamdev a066f6d
Update README
Ahmedhossamdev 3d96d0f
Add PUBLIC_ANALYTICS_API_HOST to analytics configuration in .env.example
Ahmedhossamdev d050993
Refactor PlausibleAnalytics to remove domain parameter and update API…
Ahmedhossamdev c7c9bfe
Add unit tests for PlausibleAnalytics
Ahmedhossamdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { PUBLIC_ANALYTICS_DOMAIN, PUBLIC_ANALYTICS_ENABLED } from '$env/static/public'; | ||
|
||
class PlausibleAnalytics { | ||
constructor(domain) { | ||
this.domain = domain; | ||
this.defaultProperties = {}; | ||
this.enabled = PUBLIC_ANALYTICS_ENABLED === 'true' && PUBLIC_ANALYTICS_DOMAIN !== ''; | ||
} | ||
|
||
async postEvent(pageURL, eventName, props = {}) { | ||
if (!this.enabled) { | ||
console.debug('Analytics disabled: skipping event'); | ||
return; | ||
} | ||
|
||
const payload = { | ||
domain: this.domain, | ||
name: eventName, | ||
url: pageURL, | ||
props: this.buildProps(props) | ||
}; | ||
|
||
try { | ||
console.debug('Sending event:', payload); | ||
const response = await fetch(`/api/events`, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(payload) | ||
}); | ||
if (!response.ok) { | ||
const errorText = await response.text(); | ||
throw new Error(`Error sending event: ${response.statusText}. ${errorText}`); | ||
} | ||
return response.json(); | ||
} catch (error) { | ||
console.error('Error tracking event:', error); | ||
throw error; | ||
} | ||
} | ||
|
||
async reportPageView(pageURL, props = {}) { | ||
return this.postEvent(pageURL, 'pageview', props); | ||
} | ||
|
||
async reportSearchQuery(query) { | ||
return this.postEvent('/search', 'search', { query: query }); | ||
} | ||
|
||
async reportStopViewed(id, stopDistance) { | ||
return this.postEvent('/stop', 'pageview', { id: id, distance: stopDistance }); | ||
} | ||
|
||
async reportRouteClicked(routeId) { | ||
return this.postEvent('/route', 'click', { id: routeId }); | ||
} | ||
|
||
async reportArrivalClicked(action) { | ||
return this.postEvent('/arrivals', 'click', { item_id: action }); | ||
} | ||
|
||
buildProps(otherProps = {}) { | ||
return { ...this.defaultProperties, ...otherProps }; | ||
} | ||
} | ||
|
||
const analytics = new PlausibleAnalytics(PUBLIC_ANALYTICS_DOMAIN); | ||
export default analytics; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { calcDistanceBetweenTwoPoints } from '$lib/mathUtils'; | ||
|
||
/** | ||
* Converts a distance (in km) to a category string. | ||
* @param {number} distanceKm - The distance in kilometers. | ||
* @returns {string} - The distance category string. | ||
*/ | ||
export function getDistanceCategory(distanceKm) { | ||
const distanceM = distanceKm * 1000; | ||
if (distanceM < 50) { | ||
return 'User Distance: 00000-00050m'; | ||
} else if (distanceM < 100) { | ||
return 'User Distance: 00050-00100m'; | ||
} else if (distanceM < 200) { | ||
return 'User Distance: 00100-00200m'; | ||
} else if (distanceM < 400) { | ||
return 'User Distance: 00200-00400m'; | ||
} else if (distanceM < 800) { | ||
return 'User Distance: 00400-00800m'; | ||
} else if (distanceM < 1600) { | ||
return 'User Distance: 00800-01600m'; | ||
} else if (distanceM < 3200) { | ||
return 'User Distance: 01600-03200m'; | ||
} else { | ||
return 'User Distance: 03200-INFINITY'; | ||
} | ||
} | ||
|
||
/** | ||
* Calculates the distance between the user location and the stop, | ||
* then returns the corresponding distance category for analytics. | ||
* | ||
* @param {number} userLat - User latitude. | ||
* @param {number} userLng - User longitude. | ||
* @param {number} stopLat - Stop latitude. | ||
* @param {number} stopLng - Stop longitude. | ||
* @returns {string} - The analytics distance category. | ||
*/ | ||
export function analyticsDistanceToStop(userLat, userLng, stopLat, stopLng) { | ||
const distanceKm = calcDistanceBetweenTwoPoints(userLat, userLng, stopLat, stopLng); | ||
return getDistanceCategory(distanceKm); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
export async function POST({ request }) { | ||
try { | ||
const { | ||
domain, | ||
name, | ||
url, | ||
referrer, | ||
props, | ||
apiHost = 'https://plausible.io' | ||
Ahmedhossamdev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} = await request.json(); | ||
const res = await fetch(`${apiHost}/api/event`, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
domain, | ||
name, | ||
url, | ||
referrer, | ||
props | ||
}) | ||
}); | ||
|
||
if (!res.ok) { | ||
return new Response(JSON.stringify({ error: `Error sending event: ${res.statusText}` }), { | ||
status: res.status, | ||
headers: { 'Content-Type': 'application/json' } | ||
}); | ||
} | ||
|
||
const text = await res.text(); | ||
let data; | ||
try { | ||
data = JSON.parse(text); | ||
} catch { | ||
data = { status: text }; | ||
} | ||
return new Response(JSON.stringify(data), { | ||
status: res.status, | ||
headers: { 'Content-Type': 'application/json' } | ||
}); | ||
} catch (error) { | ||
return new Response(JSON.stringify({ error: error.message || 'Unknown error' }), { | ||
status: 500, | ||
headers: { 'Content-Type': 'application/json' } | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { writable } from 'svelte/store'; | ||
|
||
export const userLocation = writable({ lat: null, lng: null }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.