@lukajekic/metrica-sdk 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +25 -6
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const METRICA_BACKEND_URL = 'https://metrica-suce.onrender.com'
2
2
 
3
- export function CreateMetricaView(APIKey) {
3
+ export async function CreateMetricaView(APIKey) {
4
4
  const path = window.location.pathname
5
5
  const sessionID = window.sessionStorage.getItem('metrica_session')
6
6
  let unique = 'false'
@@ -11,18 +11,27 @@ export function CreateMetricaView(APIKey) {
11
11
  window.sessionStorage.setItem('metrica_session', uuid)
12
12
  }
13
13
 
14
- fetch(`${METRICA_BACKEND_URL}/me
15
- trica/pageview/track?unique=${unique}`, {
14
+ let countrycode = window.sessionStorage.getItem('metrica_countrycode')
15
+ if (!countrycode) {
16
+ const response = await fetch('https://ipapi.co/json/')
17
+ const data = await response.json()
18
+ const cc_temp = data.country
19
+ window.sessionStorage.setItem('metrica_countrycode', cc_temp)
20
+ countrycode = cc_temp
21
+ }
22
+
23
+ fetch(`${METRICA_BACKEND_URL}/metrica/pageview/track?unique=${unique}`, {
16
24
  method: 'POST',
17
25
  headers: {
18
26
  "Content-Type": "application/json",
19
27
  "x-metrica-key": APIKey,
20
- "x-metrica-path": path
28
+ "x-metrica-path": path,
29
+ "x-metrica-country": countrycode
21
30
  }
22
31
  })
23
32
  }
24
33
 
25
- export function CreateMetricaEvent(APIKey, EventID) {
34
+ export async function CreateMetricaEvent(APIKey, EventID) {
26
35
  const eventID = window.sessionStorage.getItem(`metrica_${EventID}`)
27
36
  let unique = 'false'
28
37
 
@@ -31,12 +40,22 @@ export function CreateMetricaEvent(APIKey, EventID) {
31
40
  window.sessionStorage.setItem(`metrica_${EventID}`, 'toggled')
32
41
  }
33
42
 
43
+ let countrycode = window.sessionStorage.getItem('metrica_countrycode')
44
+ if (!countrycode) {
45
+ const response = await fetch('https://ipapi.co/json/')
46
+ const data = await response.json()
47
+ const cc_temp = data.country
48
+ window.sessionStorage.setItem('metrica_countrycode', cc_temp)
49
+ countrycode = cc_temp
50
+ }
51
+
34
52
  fetch(`${METRICA_BACKEND_URL}/metrica/eventtrigger/track?unique=${unique}`, {
35
53
  method: 'POST',
36
54
  headers: {
37
55
  "Content-Type": "application/json",
38
56
  "x-metrica-key": APIKey,
39
- "x-metrica-event": EventID
57
+ "x-metrica-event": EventID,
58
+ "x-metrica-country": countrycode
40
59
  }
41
60
  })
42
61
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lukajekic/metrica-sdk",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Metrica SDK connects your App with Metrica Cloud's server for monitoring traffic and Events on your website. Make sure you are using Vercel for deploying your frontend.",
5
5
  "license": "ISC",
6
6
  "author": "Luka Jekic",