feat: track and push TeleCart Pulse events
This commit is contained in:
50
frontend/spa/src/stores/Pulse.js
Normal file
50
frontend/spa/src/stores/Pulse.js
Normal file
@@ -0,0 +1,50 @@
|
||||
import {defineStore} from "pinia";
|
||||
import {ingest, saveTelegramCustomer} from "@/utils/ftch.js";
|
||||
import {toRaw} from "vue";
|
||||
import {deserializeStartParams} from "@/helpers.js";
|
||||
|
||||
export const usePulseStore = defineStore('pulse', {
|
||||
state: () => ({
|
||||
tracking_id: null,
|
||||
campaign_id: null,
|
||||
}),
|
||||
|
||||
actions: {
|
||||
initFromStartParams() {
|
||||
const webapp = window.Telegram.WebApp;
|
||||
const startParam = webapp.initDataUnsafe.start_param;
|
||||
const deserialized = deserializeStartParams(startParam);
|
||||
this.tracking_id = deserialized?.tracking_id;
|
||||
this.campaign_id = deserialized?.campaign_id;
|
||||
console.debug('[Pulse] Init with start parameters: ', deserialized);
|
||||
},
|
||||
|
||||
ingest(event, eventData = {}) {
|
||||
ingest({
|
||||
event: event,
|
||||
payload: {
|
||||
webapp: window.Telegram.WebApp,
|
||||
eventData: eventData,
|
||||
},
|
||||
})
|
||||
.then(() => console.debug('[Pulse] Event Ingested', event, eventData))
|
||||
.catch(err => console.error('Ingest failed:', err));
|
||||
},
|
||||
|
||||
catchTelegramCustomerFromInitData() {
|
||||
const userData = window.Telegram?.WebApp?.initDataUnsafe?.user;
|
||||
if (userData) {
|
||||
console.debug('[Pulse] Saving Telegram customer data');
|
||||
saveTelegramCustomer(userData)
|
||||
.then((response) => {
|
||||
this.tracking_id = this.tracking_id || response?.data?.tracking_id || null;
|
||||
console.debug(
|
||||
'[Pulse] Telegram customer data saved successfully. Tracking ID: ',
|
||||
toRaw(this.tracking_id)
|
||||
);
|
||||
})
|
||||
.catch(() => console.warn('[Pulse] Failed to save Telegram customer data:', error));
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user