Skip to content

Commit

Permalink
Merge pull request #194 from arconnectio/arc-250/balance-event-tracking
Browse files Browse the repository at this point in the history
fix: monthly ar track set to beginning of the month
  • Loading branch information
nicholaswma authored Jan 17, 2024
2 parents 89d2ebe + 01c5387 commit 9418979
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions src/utils/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ export const trackBalance = async (alarmInfo?: Alarms.Alarm) => {
);
try {
await trackDirect(EventType.BALANCE, { totalBalance });
const timer = setToNextMonth(new Date());
await ExtensionStorage.set(`balance_tracker`, timer.getTime());
const timer = setToStartOfNextMonth(new Date());
browser.alarms.create("track-balance", {
when: timer.getTime()
});
Expand All @@ -179,33 +178,38 @@ export const trackBalance = async (alarmInfo?: Alarms.Alarm) => {
}
};

export const initializeARBalanceMonitor = async () => {
// schedule monthly alarm
const alarm = await ExtensionStorage.get(`balance_tracker`);

if (alarm) {
const time = new Date(alarm);
browser.alarms.create("track-balance", {
when: time.getTime()
});
} else {
const timer = setToNextMonth(new Date());
/**
* Initializes the AR balance event tracker.
* This function sets up a monthly alarm to track the total balance.
* It schedules the first alarm to the start of the next month and
* stores this schedule time in the extension storage.
*/

browser.alarms.create("track-balance", {
when: timer.getTime()
});
await ExtensionStorage.set(`balance_tracker`, timer.getTime());
}
export const initializeARBalanceMonitor = async () => {
const timer = setToStartOfNextMonth(new Date());
browser.alarms.create("track-balance", {
when: timer.getTime()
});
};

const setToNextMonth = (currentDate: Date): Date => {
const newDate = new Date(currentDate.getTime());
const currentMonth = newDate.getMonth();
newDate.setMonth(currentMonth + 1);

if (newDate.getMonth() !== (currentMonth + 1) % 12) {
newDate.setDate(0);
}

/**
* Sets the given date to the start of the next month in UTC
* The time is set to beginning of the month (00:00:00.000).
* @param {Date} currentDate
* @returns {Date} Date to trigger alarm
*/

const setToStartOfNextMonth = (currentDate: Date): Date => {
const newDate = new Date(
Date.UTC(
currentDate.getUTCFullYear(),
currentDate.getUTCMonth() + 1,
1,
0,
0,
0,
0
)
);
return newDate;
};

0 comments on commit 9418979

Please sign in to comment.