Day of use streak counter?

Hello every one,

Another noob question here, hope you don’t mind.

I want to set a day usage streak counter for my app. So I though to go this way :

  • Set a new on device db to get dates
  • Create data variable linked to the db
  • When user first turns on the app, there is a new record with the date
  • When the user comes back, if it is the same day, nothing happens, if it is the next day, then a new record is added, if it is more than a day, the first record is deleted and replaced by this new day.
  • The streak counter is simply the length of the db / number of items recorded

I tried many ways including by formulas and using custom javascript looking up for the date hasOwnProperty or simply bracket notation to access the current day (I set a page variable named now linked to the formula NOW()] but It seems I can’t make it working to search for the date in the db and perform the sub-mentioned tasks.
I also tried a loop and recursion with custom JS (which I am still quite noob at tbh) but I think in every case, the fact I used a page variable linked to NOW() is bringing a data type that does not fit my functions and I can figure how to solve that.

Would there be a simpler way of doing a simple day of use streak counter ? Else any thing I might have missed ?

Thank you,

Small update, I tried now using the get record collection filtered with today’s date and if not found, create one but it seems not working. I keep having an empty array. So any suggestions would be welcome, I will keep on trying meanwhile.

Hello,

Your approach sounds alright, what does your get record logic look like? If the database only contains datetime objects then a formula like DATETIME_IS_SAME would probably do the trick in checking if today has already been recorded or not.

Thank you for your reply.

Actually, I don’t only need to check if tooday has been recorded but if more than a day passed since last record.

I am trying to go another way around using memory recording and I have an issue that is also bugging me for another aspect so maybe by solving it, we might get it all working :

I tried to record the very first day of use in the storage using set item to storage. Then I simply tried to compare the datetime recorded to NOW() but I keep having the error a json type file cannot be compared to a datetime. I tried accessing the json content of the data in storage but it did not work. Tried also converting to string or datetime and did not work either. So is there either a way to specify the nature of the item in storage when storing or to transform/get from the json format to get the datetime format ?
Trough this, I would just compare NOW() to the first date, if it is tooday, nothing happens, if it is one day more, I increment the streak counter in the db, if it is to more than one day, I delete the first date and create a new first date and set the streak back to zero.

Let me know if that is feasible or any alternative would be appreciated,

Thank you very much for taking time to reply,

Hey,

There is no way to specify the data type with Set item to storage, but I think you are able to use it even though the formula editor complains about the comparisons, especially if it’s just a warning and not an error that disables saving it. I would wrap the storage content into a DATETIME() as you said, then do the comparison and test it out because it might work even though the formula complains (annoying, yes I know :grin:)

I tried a bit but there is the issue that I can’t know for sure it works until I wait every day for the change which is quite tiring. I will got about this another way and see how it goes, thanks for the help, I will update with a detailed solution.

1 Like

Hello,

So I think I finaly made it work and I might submit the function to the store ! Basically, I simply used set object to memory and worked with that and multiple ifs to make it work. I also had to make sure the day was in dd/mm/yyyy and also the functions did not want to use the output of the getitemfromstorage directly so I had to use a page variable as an inbetween. Anyways it seems to work.

My main issue now is a bit more problematic and I don’t understand why appgyver is doing this :

  • I created a series of dataset inside my database by chaining “Create variable” functions one after the other in the hope that the first object get to have the position 0 in the db, the second 1, etc. But for some weird reason, it seems that the software creates them in a random order despite each of them being specifically chained to the previous one.

Right now, the third Create_record is in position 0, the first in position 6, very weird.

Would you know what is going on and how to fix this please ?

Thank you for you help !

Hey,

Hmm difficult to say, some databases create items in a “random” position within the database so it might just be a feature. You could add a property createdAt with the formula NOW() to the items if you want to be able to order the items in a specific order :slight_smile:

1 Like

Hello,

On this matter, I am using the appgyver on device database not a third party one so I seems it is not recording the items in the order they are created, I think I will simply open a bug ticket ? I will check you proposition and see how it goes to solve it meanwhile !

Thank you for all your help,

Much appreciated !