saleemkce/timeonsite

Local storage data that is sent to endpoint url is not the most recent data

Closed this issue · 3 comments

Hi @saleemkce. First I just wanted to say awesome work with this project.
I'm using the local data storage configuration option rather than the realtime callback option. However when receiving the TOS data at my endpoint, I always get the tos object of the first item of the current date key after every request/refresh. I'm assuming it's because of the line below.

params = itemData[0],
Shouldn't params be set to the last element in the itemData array? Kindly assist.

Hi @thigarette Thanks for using Timeonsitetracker.js and providing feedback:

Yes, line number 1597 contains the code,
params = itemData[0],
in latest version, i.e v1.2.1

Yes, it reads the data in Localstorage from the beginning of the array because it contains the tos older data at the beginning of the array and most recent data at the array's tail/end. This is how it's designed.

But, if you look at the code below on line 1646, it calls after sending the data at head to XHR successfully.
self.updateStorageData(dateKey, itemData);

So, inside this function, we remove the data from the entry position using .shift() method.

TimeOnSiteTracker.prototype.updateStorageData = function(dateKey, itemData) {
    var self = this;

    itemData.shift();
...
...
...
}

So, on next iteration, you will have next older data in array head and the most recent data at array's tail. Also this itemData is put into a for loop for processing the tos data, aka, time-on-site data one by one. We get this time-on-site param over time in ascending order because of this behaviour only. You can take a look at this image for parameter tos instead of parameter top (time-on-page) https://saleemkce.github.io/timeonsite/assets/img/tos_reports.png, how it starts at lower value and gets added to tos over time.

In using LocalStorage/Request object method, the documentation says, you need to send back "success" from the backend API. This is likely your problem. If it doesn't send back "success" as its response, likely it assumes there is a failure at the backend API. Hence, it doesn't remove the data at array's head position. Hence, the stale data at all time.

Please look at these examples in PHP or NodeJs here https://saleemkce.github.io/timeonsite/backend/index.html#examples , there is "success" response from server to inform the client(browser) that the data is processed at backend properly.

In case of queries, you can use free backend that can take care of this internally. The free backend code, visual is also here: https://github.com/saleemkce/visual It's just plug and play instead of you writing and configuring the backend. Let me know if you have any questions.

@saleemkce Thank you for the thorough response! This helps a lot. Let me sort out my backend and try again.

Closing the issue since it's mis-configuration on the backend API response on user part.