I’m building in-app analytics (targets) in CHT. I noticed that the date property in a target configuration supports a function (in addition to now and reported).
“A function can be used to indicate when the document should be counted.”
Question 1: What must this function return for the document to be counted?
My understanding is that it must return the current DateTime to ensure a document is counted (based on the target-emitter.js logic).
I am using this method to count reports that fall within current Bikram Sambat calendar month:
date: (_contact, report) => {
if (isCurrentMonthInBS(report.reported_date)) {
return DateTime.now().toJSDate(); // count it
}
return new Date(0); // skip
}
This gives us two ways to conditionally count a document:
Use a date function that returns current DateTime (and includes the counting logic).
Set date: 'now' and put the date logic in appliesIf.
Question 2: Is there any advantage to using one option over the other? Does the choice affect how targets are refreshed over time?
What the function returns is based on the interval that you want the target to be counted.
I’m going to give you an example of a case where you should use the function: Suppose that your target is number of children born in the current month. Your delivery form has a property that records the date of the delivery, so this date does not have to be the same as the reporting date of the form. In this case, your function should return the delivery date form property, which is neither now or the reported_date. This way your child birth will be counted in the correct interval.
Setting the date to now will make the target to always be counted, regardless of when it gets calculated. This is how you get “all-time” targets. If in the example above, the function would return the current date, then the child would be counted as being born in every target interval.
What you should remember is that all reports, regardless of their reported date, will get counted towards targets on target recalculation. Recalculation happens often, and when your target returns the current date, that will be the date of every recalculation - not a relevant value unless you want that report to be counted towards your target for every interval.
Your date function should return an actual date, resulting from a calculation based on information from your report. If you want your target to always count a report, just use now.
Thanks @diana. That clears my confusion regarding the option to use a function for the date property.
Your date function should return an actual date, resulting from a calculation based on information from your report.
In my case, the calculation is based on the report’s reported date in relation to the Bikram Sambat calendar month. Shall I move that logic to appliesIf?
I think that can work. But the assumption here is that targets will always be calculated for the current interval. This is right now correct, and it’s safe to do this.
However we have had several discussions and questions about why it is not possible to calculate targets for a previous interval. This is one of the reasons.
Re @Ben_Kiarie@jkuester for discussions during the previous months targets squad.