# How to set up In-app analytics

**URL:** <https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533>\
**Category:** Technical Support\
**Created:** [October 27, 2021, 2:06pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533 "2021-10-27T14:06:01Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![oyierphil](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/oyierphil/32/946_2.png) [@oyierphil](https://forum.communityhealthtoolkit.org/u/oyierphil)\
**Post date:** [November 19, 2021, 2:48pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/21 "2021-11-19T14:48:42Z")

</div>

jkuester,  
Thank you, I have modified my code as below:

```auto
module.exports = [
  {
    id: 'percentage-covid-test',
    type: 'percent',
    icon: 'icon-healthcare-assessment',
    goal: -1,
    translation_key: 'targets.covid.title',
    subtitle_translation_key: 'targets.this_month.subtitle',
    appliesTo: 'contacts',
    appliesToType: ['person'],  
    passesIf: function covidPercTested(contact)
  {
      return contact.reports.some(report => report.form === 'screening' && report.fields.cov === 'true');
  }
  
    date:'reported'
  },

];

```

I get `..../targets.js. SyntaxError: Unexpected identifier`

Now that we have the function defined, I think we don’t need to define any function in the `nools-extras.js`file

Still getting errors, need to figure this out today and define targets for the other items  
If it works for Covid, it will work for the others

---

<div class="post-metadata">

**Author:** ![jkuester](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/jkuester/32/806_2.png) [@jkuester](https://forum.communityhealthtoolkit.org/u/jkuester)\
**Post date:** [November 19, 2021, 2:53pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/22 "2021-11-19T14:53:17Z")

</div>

> [@oyierphil](#):
>
> Now that we have the function defined, I think we don’t need to define any function in the `nools-extras.js`file

Right. If you just want to define the `passesIf` function inline, you can just put:

```auto
passesIf: function(contact)
{
return contact.reports.some(report => report.form === ‘screening’ && report.fields.cov === ‘true’);
},

```

(Don’t forget the comma before `date:'reported'`)

---

<div class="post-metadata">

**Author:** ![oyierphil](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/oyierphil/32/946_2.png) [@oyierphil](https://forum.communityhealthtoolkit.org/u/oyierphil)\
**Post date:** [November 19, 2021, 3:47pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/23 "2021-11-19T15:47:51Z")

</div>

jkuester  
For ease of adding the other target values later, I went back to declaration in target and definition in `nools-extras` as below:  
**target.js**

```auto
const extras = require('./nools-extras');
const {
  covidPercTested,
  } = extras; 

module.exports = [
  {
    id: 'percentage-covid-test',
    type: 'percent',
    icon: 'icon-healthcare-assessment',
    goal: -1,
    translation_key: 'targets.covid.title',
    subtitle_translation_key: 'targets.this_month.subtitle',

    appliesTo: 'contacts',
    appliesToType: ['person'],
    
    passesIf: covidPercTested,
  
    date:'reported',
  },

];

```

**nools-extras**

```auto
function covidPercTested(contact)
{
  return contact.reports.some(report => report.form === 'screening' && report.fields.cov === 'true');
},

module.exports = {
  covidPercTested,
};

```

I still get an error, have to figure this out before I leave for home today  
…/targets.js. SyntaxError: Unexpected token ,

---

<div class="post-metadata">

**Author:** ![oyierphil](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/oyierphil/32/946_2.png) [@oyierphil](https://forum.communityhealthtoolkit.org/u/oyierphil)\
**Post date:** [November 19, 2021, 4:24pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/24 "2021-11-19T16:24:35Z")

</div>

![CHT Target Errors_3](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/6/6977afcc8a39bc473b3dc41cb3862523cb8f44d9.png)

---

<div class="post-metadata">

**Author:** ![jkuester](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/jkuester/32/806_2.png) [@jkuester](https://forum.communityhealthtoolkit.org/u/jkuester)\
**Post date:** [November 19, 2021, 5:12pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/25 "2021-11-19T17:12:34Z")

</div>

I was able to recreate your error. I think it is being caused by the extra comma that you have in your `nools-extras.js` file. At the end of the `covidPercTested` and before the `module.exports = {` line you have:

```auto
},

```

when It should just be:

```auto
}

```

One thing that might be helpful when trying to track down these syntax errors would be to just parse your `targets.js` file with Node (e.g. just run `node targets.js`). If there is a syntax error, the output from node will be a lot more helpful in identifying it then the output from cht-conf. Here is the output that I got:

```auto
❯ node targets.js
/home/jlkuester7/git/cht-core/config/covid-19/nools-extras.js:46
},
 ^

SyntaxError: Unexpected token ','
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/jlkuester7/git/cht-core/config/covid-19/targets.js:1:16)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)

```

---

<div class="post-metadata">

**Author:** ![oyierphil](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/oyierphil/32/946_2.png) [@oyierphil](https://forum.communityhealthtoolkit.org/u/oyierphil)\
**Post date:** [November 21, 2021, 2:16pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/26 "2021-11-21T14:16:07Z")

</div>

Dear jkuester  
Thank you, our server is currently down due to power issues, thus not able to test, will test tomorrow  
Wanted to create analytics for the whole National Office (Totals for the project) and the four branches (County1, County2,County3, County4), any ideas

---

<div class="post-metadata">

**Author:** ![oyierphil](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/oyierphil/32/946_2.png) [@oyierphil](https://forum.communityhealthtoolkit.org/u/oyierphil)\
**Post date:** [November 22, 2021, 10:02am UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/27 "2021-11-22T10:02:03Z")

</div>

Dear jkuester  
From my understanding so far based on feedback and online documentation, I only need to configure `targets.js` and ‘nools-extras.js’ for my dashboard widgets. I’ve seen and read about the file tasks.js and now confused, do I need to configure this for the analytics dashboard to work?

My errors aren’t ending and I’m figuring out why, we have a presentation of the dashboard tomorrow and will appreciate any ideas

---

<div class="post-metadata">

**Author:** ![jkuester](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/jkuester/32/806_2.png) [@jkuester](https://forum.communityhealthtoolkit.org/u/jkuester)\
**Post date:** [November 22, 2021, 7:37pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/28 "2021-11-22T19:37:53Z")

</div>

@oyierphil I am sorry to hear that things are not working for you yet. You should not need any special configuration for your `tasks.js` to have working targets on your dashboard. I tried the following `targets.js` code on a test CHT instance and the widget displayed successfully for both a CHW user and a supervisor.

```auto
const covidPercTested = (contact) =>
{
  return contact.reports.some(report => report.form === 'screening' && report.fields.cov === 'true');
};

module.exports = [
  {
    id: 'percentage-covid-test',
    type: 'percent',
    icon: 'icon-healthcare-assessment',
    goal: -1,
    translation_key: 'targets.covid.title',
    subtitle_translation_key: 'targets.this_month.subtitle',
    appliesTo: 'contacts',
    appliesToType: ['person'],
    passesIf: covidPercTested,
    date: 'reported',
  },
];

```

![covid_screenshot](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/1/1546da67a54c4e3208d310b56db83e9cf15af581.png)

Regarding the analytics for the whole National Office, something at that scale might be outside the scope of what is recommended for the `targets.js` file. This is outside my experience, so I cannot say for sure. But I will reach out to some other folks and see if they can weigh in here on the best way to do analytics at that scale.

---

<div class="post-metadata">

**Author:** ![jkuester](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/jkuester/32/806_2.png) [@jkuester](https://forum.communityhealthtoolkit.org/u/jkuester)\
**Post date:** [November 22, 2021, 9:12pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/29 "2021-11-22T21:12:40Z")

</div>

Okay, so I was able to confirm with my colleagues that the targets defined in the `targets.js` file are really only intended to be used at the level of CHWs and their supervisors. Trying to use that framework to analyze very large numbers of documents (such as for the whole National Office) can cause performance issues.

For reporting and analytics on that large of a scale, we recommend sending the CHT data to a PostgreSQL database and then using a third-party visualization platform to create the desired dashboards based on that data. That process is described [here in the CHT docs](https://docs.communityhealthtoolkit.org/core/overview/data-flows-for-analytics/).

---

<div class="post-metadata">

**Author:** ![kitsao](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/kitsao/32/57_2.png) [@kitsao](https://forum.communityhealthtoolkit.org/u/kitsao)\
**Post date:** [November 23, 2021, 12:35pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/30 "2021-11-23T12:35:53Z")

</div>

Hey @oyierphil how did it go after the session we had today?

---

<div class="post-metadata">

**Author:** ![oyierphil](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/oyierphil/32/946_2.png) [@oyierphil](https://forum.communityhealthtoolkit.org/u/oyierphil)\
**Post date:** [November 23, 2021, 12:51pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/31 "2021-11-23T12:51:40Z")

</div>

Kitsao  
After pushing the targets.js and nools.js to both the test and production servers, the widgets are available on the test server but not showing data, while production server doesn’t show even the widgets from the form as below

 ![CHT Target Errors_4](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/5/5450fe5b68074f0e27127c50c4e4834b6345bbeb.png);

I have also noted despite pushing the same forms to both the production and test servers, the Covid results are displayed in the downloaded data for the test server while the same is not displayed in the downloaded data for the production server as shown below:

 ![CHT Form Data Export Errors](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/5/5dd09caab13d8716921d22e5edb9be26c4793ef5.png)

Trying to figure out why, I can see the data captured but can’t see the same when I download?

---

<div class="post-metadata">

**Author:** ![oyierphil](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/oyierphil/32/946_2.png) [@oyierphil](https://forum.communityhealthtoolkit.org/u/oyierphil)\
**Post date:** [November 23, 2021, 12:57pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/32 "2021-11-23T12:57:25Z")

</div>

jkuester  
Thank you very much, I actually managed to debug the codes yesterday and see the widget  
I have established that to pull data from the forms for targets, we need the **formID** , then **groupname** and finally **fieldname** for our case (Would appreciate any link to this)

Thus the widget is displayed on my test server and not on the production, and no data in both instances, the struggle continues 😅

---

<div class="post-metadata">

**Author:** ![kitsao](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/kitsao/32/57_2.png) [@kitsao](https://forum.communityhealthtoolkit.org/u/kitsao)\
**Post date:** [November 23, 2021, 4:27pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/33 "2021-11-23T16:27:22Z")

</div>

@oyierphil I’ll point you to @jkuester 's comment above. Check that the role you want to avail in-app analytics to has `can_view_analytics`, `can_view_analytics_tab` and that they have direct access to reports of the type `<form-id>` you use in your targets. I think that would be the `rapid response officer` role. Also ensure that the contact linked to the account you’re testing with belongs to a place served directly by the role e.g. rapid response team in your case. Let us know how it goes.

---

<div class="post-metadata">

**Author:** ![oyierphil](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/oyierphil/32/946_2.png) [@oyierphil](https://forum.communityhealthtoolkit.org/u/oyierphil)\
**Post date:** [November 23, 2021, 7:08pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/34 "2021-11-23T19:08:03Z")

</div>

Kitsao  
We tried all the roles, online and offline, data still not displayed  
Do we have a situation where the widgets are visible but data not displayed because of roles and hierarchy?  
What I have seen is targets either visible or not visible because of roles and hierarchy?

---

<div class="post-metadata">

**Author:** ![jkuester](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/jkuester/32/806_2.png) [@jkuester](https://forum.communityhealthtoolkit.org/u/jkuester)\
**Post date:** [November 23, 2021, 10:00pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/35 "2021-11-23T22:00:36Z")

</div>

> Do we have a situation where the widgets are visible but data not displayed because of roles and hierarchy?

Data will only be displayed for persons accessible to the user. (So for a given CHW, the widget would only display data for _their_ patients.) Otherwise, I don’t think there is anything that would prevent a user that can see the widget from seeing data for patients accessible to them.

Did you end up implementing an `appliesIf` function in your target? If something is wrong with that function it might cause the `0 of 0` behavior in your screenshot.

---

<div class="post-metadata">

**Author:** ![kitsao](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/kitsao/32/57_2.png) [@kitsao](https://forum.communityhealthtoolkit.org/u/kitsao)\
**Post date:** [November 30, 2021, 1:23pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/36 "2021-11-30T13:23:25Z")

</div>

Hey @oyierphil how did it go?

---

<div class="post-metadata">

**Author:** ![oyierphil](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/oyierphil/32/946_2.png) [@oyierphil](https://forum.communityhealthtoolkit.org/u/oyierphil)\
**Post date:** [December 1, 2021, 10:28am UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/37 "2021-12-01T10:28:08Z")

</div>

jkuester  
We are preparing for end of semester exams, thus took a break, I still cant be able to display the data, only widgets available, and my code is as below:  
_target config_

```auto
const extras = require('./nools-extras');

const {
  posCovidPercTested,
  negCovidPercTested,
 } = extras; 

module.exports = [  
  {
    id: 'percentage-positive-covid-test',
    type: 'percent',
    icon: 'medic-covid-case',
    goal: -1,
    translation_key: 'targets.posCovid.title',
    subtitle_translation_key: 'targets.this_month.subtitle',
    appliesTo: 'contacts',
    appliesToType: ['person'],
    passesIf: posCovidPercTested,
    date:'reported',
  },

  {
    id: 'percentage-negative-covid-test',
    type: 'percent',
    icon: 'medic-covid-case',
    goal: -1,
    translation_key: 'targets.negCovid.title',
    subtitle_translation_key: 'targets.this_month.subtitle',
    appliesTo: 'contacts',
    appliesToType: ['person'],
    passesIf: negCovidPercTested,
    date:'reported',
  },

];

```

_nools-extra_

```auto
const posCovidPercTested = contact => {
  return contact.reports.some(report => report.form === 'case_investigation' && report.fields.testing.cov_test === 'Positive');
};

const negCovidPercTested = contact => {
  return contact.reports.some(report => report.form === 'case_investigation' && report.fields.testing.cov_test === 'Negative');
};

module.exports = {
  posCovidPercTested,
  negCovidPercTested,
 }; 

```

On it now and was following your thread, Did you end up implementing an `appliesIf` function in your target? **No I didn’t**  
Given I just want to get percentage of people with Covid, how will the appliesif function look like?  
Trying to figure this out

---

<div class="post-metadata">

**Author:** ![oyierphil](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/oyierphil/32/946_2.png) [@oyierphil](https://forum.communityhealthtoolkit.org/u/oyierphil)\
**Post date:** [December 1, 2021, 2:00pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/38 "2021-12-01T14:00:29Z")

</div>

**form\_id: case\_investigation**  
begin group testing   
select\_one test cov\_test   
end group

**Test Choices**  
test Positive  
test Negative

Tried some ideas and so far, no data shown, back to research, will appreciate any guide on what I am missing

---

<div class="post-metadata">

**Author:** ![oyierphil](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/oyierphil/32/946_2.png) [@oyierphil](https://forum.communityhealthtoolkit.org/u/oyierphil)\
**Post date:** [December 3, 2021, 2:16pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/39 "2021-12-03T14:16:58Z")

</div>

Spent time yesterday looking at the server logs and didn’t find any errors, still stuck

---

<div class="post-metadata">

**Author:** ![jkuester](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/jkuester/32/806_2.png) [@jkuester](https://forum.communityhealthtoolkit.org/u/jkuester)\
**Post date:** [December 3, 2021, 6:56pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/40 "2021-12-03T18:56:00Z")

</div>

Okay @oyierphil I think I figured out our problem here. After a bunch more debugging this morning (and some help from @diana 👍) I realized that the issue is probably with the `date:‘reported’` configuration.

The [targets.js documentation](https://docs.communityhealthtoolkit.org/apps/reference/targets/) says that when `date` is set to `reported`, “the target will count documents with a `reported_date` within the current month.”

The thing that I missed when reading that earlier is that, for targets with `appliesTo: 'contacts'`, it is actually the `reported_date` on the _contact_ doc that is checked (and not the `reported_date` on the reports for the contact). So, by having `date:‘reported’` set for your targets, you are making those targets only consider contacts that were added _this month_.

Switching to `date:'now'` should make the targets consider all contacts (regardless of their `reported_date`). Then, you can update your `posCovidPercTested/negCovidPercTested` functions to filter the reports by their own `reported_date` so that you only apply if the report was in the last month (or whatever time range you define).

[Previous page](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533.md?page=1)

[Next page](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533.md?page=3)
