Skip to content

Commit

Permalink
chore: adding condition for countries which are received as null
Browse files Browse the repository at this point in the history
  • Loading branch information
hirad-deriv committed Oct 18, 2024
1 parent 28a17e8 commit a486bb5
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ export function createAnalyticsInstance(options?: Options) {
let CloudflareCountry = ''
try {
const response = await fetch('https://www.cloudflare.com/cdn-cgi/trace')

if (response.ok) {
const text = await response?.text()
CloudflareCountry = Object.fromEntries(text.split('\n').map(v => v.split('=', 2))).loc.toLowerCase()
} else {
console.warn(`HTTP warning! status: ${response.status}`)
const entries = Object.fromEntries(text.split('\n').map(v => v.split('=', 2)))

if (entries.loc) {
CloudflareCountry = entries.loc.toLowerCase()
} else {
console.warn('Location not found in the response.')
}
}
} catch (error) {
console.warn('Cannot get the Cloudflare location:', error)
Expand Down

0 comments on commit a486bb5

Please sign in to comment.