# 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:** 1

<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:** [October 27, 2021, 2:06pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/1 "2021-10-27T14:06:01Z")

</div>

Hi Fred  
Was looking for a way of of getting analytics on how many cases have been registered, tested, positive and negative from the forms filled per day and exploring the following:

1. Did the suggested method work for you? What analytics does telemetry provide?
2. How do you activate analytics tab on the Medic Dashboard (Target and target aggregates)?  
Hope to configure our instance tomorrow to receive analytics data, will appreciate any feedback, thank you

---

<div class="post-metadata">

**Author:** ![derick](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/derick/32/78_2.png) [@derick](https://forum.communityhealthtoolkit.org/u/derick)\
**Post date:** [October 28, 2021, 7:33am UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/2 "2021-10-28T07:33:54Z")

</div>

@oyierphil we have a tutorial that may help [Building Target Widgets | Community Health Toolkit](https://docs.communityhealthtoolkit.org/apps/tutorials/targets/). Have you gone through that? Did you face any challenges?

> How do you activate analytics tab on the Medic Dashboard (Target and target aggregates)?

The role needs to have the `can_view_analytics` and `can_view_analytics_tab` [permissions](https://docs.communityhealthtoolkit.org/apps/reference/app-settings/user-permissions/) set.

We also have some examples (through a bit dated) in our [default config](https://github.com/medic/cht-core/blob/master/config/default/targets.js)

---

<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:** [October 28, 2021, 9:55am UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/3 "2021-10-28T09:55:12Z")

</div>

Derick  
I had gone through the tutorial and just done the steps again now, configuration is done correctly but the analytics tab doesn’t show anything as shown below. I’m using a test VM for this exercise

Want to review further the target widget settings, and re-run

 ![Target Errors](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/7/75b2dd2b0a9441e9124dbb0d196ff712e0233f0f.png)  
 ![Target Errors2](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/6/67937bf8e0e6bf84704845c08b7d45827a600fa0.png)

---

<div class="post-metadata">

**Author:** ![derick](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/derick/32/78_2.png) [@derick](https://forum.communityhealthtoolkit.org/u/derick)\
**Post date:** [October 28, 2021, 10:04am UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/4 "2021-10-28T10:04:43Z")

</div>

Are you by chance using an admin account to log in to your local instance? You may be experiencing this [Targets are disabled for admin users. If you need to see targets, login as a normal user](https://forum.communityhealthtoolkit.org/t/targets-are-disabled-for-admin-users-if-you-need-to-see-targets-login-as-a-normal-user/912)

---

<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:** [October 28, 2021, 10:26am UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/5 "2021-10-28T10:26:54Z")

</div>

Derick  
Normal user is which role? Tried with Program Officer role and same message, “Targets are disabled for admin users”  
Trying to review this code and relate to our instance before re-running:  
module.exports = [  
{  
id: ‘assessments-all-time’,  
type: ‘count’,  
icon: ‘icon-healthcare-assessment’,  
goal: -1,  
translation\_key: ‘targets.assessments.title’,  
subtitle\_translation\_key: ‘targets.all\_time.subtitle’,

```
appliesTo: 'reports',
appliesToType: ['assessment'],
date: 'now'

```

},  
{  
id: ‘assessments-this-month’,  
type: ‘count’,  
icon: ‘icon-healthcare-assessment’,  
goal: -1,  
translation\_key: ‘targets.assessments.title’,  
subtitle\_translation\_key: ‘targets.this\_month.subtitle’,

```
appliesTo: 'reports',
appliesToType: ['assessment'],
date: 'reported'

```

}  
];

```auto

```

---

<div class="post-metadata">

**Author:** ![derick](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/derick/32/78_2.png) [@derick](https://forum.communityhealthtoolkit.org/u/derick)\
**Post date:** [October 28, 2021, 10:42am UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/6 "2021-10-28T10:42:18Z")

</div>

> [@oyierphil](#):
>
> Normal user is which role?

Normal would mean a non-admin user that is restricted to their place.

For your situation, you would need to make the `Program Officer` role offline.

In app\_settings.json (assuming your local app\_settings is in sync with what is uploaded on your instance, you can modify the role as shown below: (modify to match your local naming)

```auto

"roles": {
    "program_officer": {
      "name": "usertype.program-officer",
      "offline": true
    },
  },

```

---

<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 18, 2021, 1:58pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/7 "2021-11-18T13:58:35Z")

</div>

Derick  
Is it possible to get a sample form and analytics target file for the same?  
Figuring out how to link the _id_ property of targets to the actual form fields, then I should be good to 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:** [November 18, 2021, 7:46pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/8 "2021-11-18T19:46:34Z")

</div>

Like looking at the code below:

// BIRTHS THIS MONTH  
{  
id: ‘births-this-month’,  
type: ‘count’,  
icon: ‘infant’,  
goal: -1,  
translation\_key: ‘targets.births.title’,  
subtitle\_translation\_key: ‘targets.this\_month.subtitle’,

```
appliesTo: 'reports',
appliesIf: isHealthyDelivery,
date: 'reported',

```

},  
Which section picks data from our forms? There is something I am not getting, the link between the target values and data collected through the forms, any help?

---

<div class="post-metadata">

**Author:** ![binod](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/binod/32/60_2.png) [@binod](https://forum.communityhealthtoolkit.org/u/binod)\
**Post date:** [November 19, 2021, 4:07am UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/9 "2021-11-19T04:07:37Z")

</div>

1. `appliesTo: 'reports'`

Since your target applies to reports, it counts all the reports that match the condition specified with `appliesIf`.

1. `appliesIf: isHealthyDelivery`

The function `isHealthyDelivery` must be defined somewhere accessible. If we look at [this example from the standard config](https://github.com/medic/cht-core/blob/master/config/standard/nools-extras.js#L187):

```auto
function isHealthyDelivery(c, r) {
  return r.form === 'D' ||
      (r.form === 'delivery' && r.fields.pregnancy_outcome === 'healthy');
}

```

Here, it will return `true` for all `D` and `delivery` reports which have the field `pregnancy_outcome` equal to `healthy`. So only those reports will be counted towards the target. You can change it to match your requirements.

1. `date: 'reported'`  
This means that the target will count reports with a `reported_date` within the current month.

We have a [reference page for `targets.js`](https://docs.communityhealthtoolkit.org/apps/reference/targets/) that might be helpful to learn about these different fields.

---

<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, 5:03am UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/10 "2021-11-19T05:03:08Z")

</div>

Dear Binod  
I understand the fields from the online documentation, what I missed is the link between targets and actual fields on the different forms. I see the function definition, and I see it picks both the form and field

For example we have been screening some target population for TB, Covid and PCR in different regions, thus our hierarchy is two level, one root with four children.

We only have three (3) forms (Contact creating and editing, case investigation with the screening algorithm), thus I want to create targets and visualize percentage tested and percentage screened so far both from the root (National Office) and the four branches (County1, County2,County3, County4 )

Let me figure it out, will create a function percTested() and percScreened(), will revert once I test

I also saw errors with indentation while testing targets yesterday, does one tab space work or normally spacing?

---

<div class="post-metadata">

**Author:** ![binod](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/binod/32/60_2.png) [@binod](https://forum.communityhealthtoolkit.org/u/binod)\
**Post date:** [November 19, 2021, 5:16am UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/11 "2021-11-19T05:16:59Z")

</div>

We use 2 spaces for indenting. You can get our coding style guide [here](https://docs.communityhealthtoolkit.org/contribute/code/style-guide/).

---

<div class="post-metadata">

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

</div>

If you need a percentage target, you need this information:

1. What is the denominator? (counted with `appliesIf`)
2. What is the numerator? (counted with `passesIf`)

For your percentage screening, let’s make some assumptions:

1. Denominator: Count of all contacts created this month
2. Numerator: Count of contacts from the denominator (who made through `appliesIf`), and having at least one screening report

Please note the following things:

1. The target applies to `contacts`, not to the `reports`.
2. You might want to only look for contacts who are persons, but not count the health workers or other staff.
3. We can see get the reports for the contact with `contact.reports` and determine whether to count the contact or not, based on the reports.

Then your target config might have something like this:

```auto
   ...
    type: 'percent',
    appliesTo: 'contacts',
    appliesToType: ['person'],
    appliesIf: function (contact) {
      //skipping CHWs
      return contact.role !== 'chw';
    },
    passesIf: function (contact) {
      //If there is a screening report, count the contact as pass
      return contact.reports.some(report => report.form === 'screening');
    },
   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, 7:24am UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/13 "2021-11-19T07:24:30Z")

</div>

Binod  
I have tried the following code for only Covid targets:  
module.exports = [  
{  
id: ‘percentage-covid-test’,  
type: ‘percent’,  
icon: ‘icon-healthcare-assessment’,  
goal: -1,  
translation\_key: ‘targets.covid\_test.title’,  
subtitle\_translation\_key: ‘targets.this\_month.subtitle’,

```
appliesTo: 'contacts',
appliesToType: ['person'],
appliesIf: covidPercTested,

date: 'reported'

```

},

];

And the following for nools-extra  
module.exports = {  
covidPercTested(c,r) {  
return r.form === ‘cov’ && r.fields.screening\_cov === ‘true’);  
},  
I get the error below:

 ![CHT Target Errors](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/3/3e66eca578e7937fd611ca433b41b2481574d976.png)

---

<div class="post-metadata">

**Author:** ![binod](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/binod/32/60_2.png) [@binod](https://forum.communityhealthtoolkit.org/u/binod)\
**Post date:** [November 19, 2021, 9:52am UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/14 "2021-11-19T09:52:33Z")

</div>

@oyierphil, you are getting a reference error. It means that the function `covidPercTested` is not accessible from your `targets.js`. In order to get the reference correctly, you need to export the function from `nools-extras.js` and import it in `targets.js`.

Please see this example from the default config where the function `isAlive` is defined [here](https://github.com/medic/cht-core/blob/400fd7658948160b88d2933c1032b3becbe3bae5/config/default/nools-extras.js#L11), exported [here](https://github.com/medic/cht-core/blob/400fd7658948160b88d2933c1032b3becbe3bae5/config/default/nools-extras.js#L293) and then imported [here](https://github.com/medic/cht-core/blob/400fd7658948160b88d2933c1032b3becbe3bae5/config/default/targets.js#L3).

Also, since your target now applies to the `contacts`, you might want to check the contacts instead of reports that match your criteria. For targets applying to contacts, the `report` will always be `undefined` in the `appliesIf(contact, report)` function.

Something like this might work, but you might want to use it in `passesIf` instead:

```auto
covidPercTested(contact) {
  return contact.reports.some(r => r.form === ‘cov’ && r.fields.screening_cov === ‘true’);
},

```

---

<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, 12:52pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/15 "2021-11-19T12:52:53Z")

</div>

Binod  
I read and re-wrote my code again, I get an error which I cant see  
**target file**  
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\_test.title’,  
subtitle\_translation\_key: ‘targets.this\_month.subtitle’,

```
appliesTo: 'contacts',
appliesToType: ['person'],

appliesIf: covidPercTested,

passesIf: covidPercTested(contact)

```

{  
return contact.reports.cov(report =\> report.form === ‘screening’);  
},

```
date: 'reported'

```

},

];

_nools-extras.js_

function covidPercTested(contact) {  
{  
return report.form === ‘sampling’ && report.fields.screening\_cov === ‘true’);  
},

module.exports = {  
covidPercTested,  
};

 ![CHT Target Errors_2](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/1/11131a0c65f5e329918f1920b01def34a3361b52.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, 1:49pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/16 "2021-11-19T13:49:47Z")

</div>

@oyierphil I have not taken a close look at your logic here, but I think the source of your SyntaxError is that I think you mean to use `function` instead of `covidPercTested` in this section:

```auto
    passesIf: covidPercTested(contact)
    {
      return contact.reports.cov(report => report.form === 'screening');
    },

```

So, it would just read like this:

```auto
    passesIf: function(contact)
    {
      return contact.reports.cov(report => report.form === 'screening');
    },

```

---

<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, 1:58pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/17 "2021-11-19T13:58:43Z")

</div>

jkuester,  
I made the correction and got the same error, ’ SyntaxError: Unexpected token )’  
Seem not to be able to point where the error is

Trying to review if I picked the correct details from my form for the following: contact.reports.X and  
report =\> report.form  
I have the following field from the forms, screening.cov from which I want to set the targets for Covid  
Regards

---

<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:09pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/18 "2021-11-19T14:09:15Z")

</div>

Out of curiosity, is your SyntaxError actually coming from the `nools-extras.js` file now?

```auto
  ERROR Failed to parse file /var/Bodaboda_Test/config-boda-moh-ke-master/nools-extras.js. SyntaxError: Unexpected token )

```

I noticed in your code above for the `covidPercTested` function it looked like there was an extra `)` (and a few other syntax issues). So

```auto
function covidPercTested(contact) {
{
  return report.form === 'sampling' && report.fields.screening_cov === 'true');
},

module.exports = {
  covidPercTested,
};

```

should actually be something like:

```auto
function covidPercTested(contact)
{
  return report.form === 'sampling' && report.fields.screening_cov === 'true';
}

module.exports = {
  covidPercTested,
};

```

That may fix the SyntaxErrors, but is still seems like there is something wrong with the logic in that `covidPercTested` function (since you are not actually loading anything off the `contact`). Maybe you intended that function to look similar to the one Binod suggested above:

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

```

---

<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:25pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/19 "2021-11-19T14:25:59Z")

</div>

jkuester  
Trying to figure out what “contact.reports.some” mean  
What value does report.form === X take and report.fields.Y  
Going back to basics now, appreciate the support

---

<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:42pm UTC](https://forum.communityhealthtoolkit.org/t/how-to-set-up-in-app-analytics/1533/20 "2021-11-19T14:42:35Z")

</div>

[Array.some(…)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) basically just checks to see if any of the elements in the array match the given test. So in this case `covidPercTested` would return true for any contact that had _at least one report_ matching `report.form === 'sampling' && report.fields.screening_cov === 'true'`. (That check gets run for each `report` entry in the `contact.reports` array.)

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