# Campaigns NQL examples

{% hint style="info" %}
Refer to the [Launching campaign programs](https://docs.nexthink.com/platform/user-guide/campaigns/managing-campaigns/launching-campaign-programs) documentation for guidelines on planning, gaining approval, testing and launching your campaign within your organization.
{% endhint %}

This list of NQL query examples is designed to help you select the users relevant to your campaign. Go through the queries below, copy the query that is most similar to your use case, and adjust it accordingly.

Example queries to target users in scheduled campaigns\ <a href="#campaignsnqlexamples-examplequeriestotargetusersinscheduledcampaigns" id="campaignsnqlexamples-examplequeriestotargetusersinscheduledcampaigns"></a>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<details>

<summary>Retrieve all users from a specific city.</summary>

{% code lineNumbers="true" %}

```
users
| where ad.city == "Lausanne"
```

{% endcode %}

</details>

<details>

<summary>Retrieve users with installed Collector (xTray) that has been running on their device in the last 24 hours.</summary>

{% code lineNumbers="true" %}

```
users
| include execution.events during past 24h
| where binary.name == "nxtray*"
| compute c1 = binary.name.count()
| where c1 > 0
```

{% endcode %}

</details>

<details>

<summary>Retrieve users who have installed MS Teams or Zoom in the last day.</summary>

{% code lineNumbers="true" %}

```
users
| with package.installations during past 1d
| where package.name in [ "MS Teams", "Zoom" ]
```

{% endcode %}

</details>

<details>

<summary>Retrieve users from a specific department.</summary>

{% code lineNumbers="true" %}

```
users
| where ad.department == "Finance"
```

{% endcode %}

</details>

<details>

<summary>Retrieve the last user of a device that has had 3 system crashes in the past 7 days.</summary>

{% code lineNumbers="true" %}

```
devices past 7d
| with device_performance.system_crashes past 7d
| compute nb_crashes = count()
| where nb_crashes >= 3
| with session.events past 7d
| compute user_sid = user.sid.last()
| list user_sid
```

{% endcode %}

</details>

<details>

<summary>Retrieve last users not from the IT department who has a poor battery health.</summary>

{% code lineNumbers="true" %}

```
devices past 7d
| where remote_action.get_battery_status.execution.outputs.BatteryHealth < 0.3
| with session.events past 7d
| where user.ad.department != "*IT*"
| compute user_sid = user.sid.last()
| list user_sid
```

{% endcode %}

</details>

<details>

<summary>Schedule a DEX campaign</summary>

To randomly select a different sample of employees to continuously collect the sentiment data, set up the query Below. Replace `#dex_campaign_name` with the **NQL ID** of the campaign and adjust the limit below set as `50` to specify the number of new users the campaign targets every hour.

Note that you need to:

* Define the **Schedule** trigger method on the [General tab](https://docs.nexthink.com/platform/user-guide/managing-campaigns/creating-campaigns#accessing-campaign-edit-page).
* Select **Again after** from the [Target employees will receive the campaign](#target-employees-will-receive-the-campaign) section and provide the minimum number of days you want between responses for each employee.
* Save the campaign at least once before using the NQL query below. This ensures the system recognizes your campaign and avoids a *Table does not exist* error during NQL query validation.

{% code lineNumbers="true" %}

```
users
| include campaign.#dex_campaign_name.responses
| compute number_of_requests = count()
| sort number_of_requests asc
| limit 50
```

{% endcode %}

</details>

Example queries to inspect campaign results\ <a href="#campaignsnqlexamples-examplequeriestoinspectcampaignresults" id="campaignsnqlexamples-examplequeriestoinspectcampaignresults"></a>
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<details>

<summary>All responses</summary>

All responses for the `Remote Work Demo` campaign, displaying the `username`, `time`, `state` and `state_details`, all per user.

{% code lineNumbers="true" %}

```
campaign.responses
| where campaign.name == "Remote Work Demo"
| list user.name, response.time, state, state_details
```

{% endcode %}

</details>

<details>

<summary>Summary view</summary>

Summary view of the `Remote Work Demo` campaign responses in terms of campaign `trigger_method`, `time`, `state`, `state_details` and the number of questions answered, all per user.

{% code lineNumbers="true" %}

```
campaign.responses
| where campaign.name == "Remote Work Demo"
|list response.trigger_method, user.name, response.time, campaign.response.first_planned, campaign.response.first_targeted, campaign.response.first_displayed, state, state_details, response.number_of_answered_questions
```

{% endcode %}

</details>

<details>

<summary>Count responses per state</summary>

Count campaign responses for the `Remote Work Demo` campaign by `user`, `state` and `state_details`.

{% code lineNumbers="true" %}

```
campaign.responses
| where campaign.name == "Remote Work Demo"
| summarize cnt = count() by user.name, campaign.response.state, campaign.response.state_details 
| list user.name, cnt, campaign.response.state, campaign.response.state_details   
```

{% endcode %}

</details>

<details>

<summary>Count responses for a specific user</summary>

Count campaign responses for a specific user by `campaign`, `state` and `state_details`.

{% code lineNumbers="true" %}

```
campaign.responses
| where user.name == "johndoe@DOMAIN"
| summarize cnt = count() by campaign.name, user.name, campaign.response.state, campaign.response.state_details   
| list user.name, campaign.name, cnt, campaign.response.state, campaign.response.state_details 
```

{% endcode %}

</details>

<details>

<summary>Historical states</summary>

View the historical states of the campaign responses for the `Remote Work Demo` campaign.

{% code lineNumbers="true" %}

```
campaign.responses
| where campaign.name == "Remote Work Demo"
| list response.historical_time, response.historical_state, response.historical_state_details
```

{% endcode %}

</details>

<details>

<summary>Count the number of answered or declined responses</summary>

Count the number of answered or declined responses of a campaign with the `#my_campaign` NQL ID.

{% code lineNumbers="true" %}

```
campaign.#my_campaign.responses
| where state in [answered, declined]
| summarize cnt_responses = request_id.count() by state
| list state, cnt_responses
```

{% endcode %}

</details>

<details>

<summary>Count the number of responses by question choice</summary>

Count the number of responses by question choice for a single-choice, opinion-scale or NPS question with the `vpn_quality` question ID.

{% code lineNumbers="true" %}

```
campaign.#my_campaign.responses
| summarize cnt = request_id.count() by answers.vpn_quality.label, state, state_details
| sort cnt desc
```

{% endcode %}

</details>

<details>

<summary>List users who answered a specific campaign</summary>

List the users who answered a campaign with the `#my_campaign` NQL ID.

{% code lineNumbers="true" %}

```
campaign.#my_campaign.responses
| where state == answered
| list user.name, state, state_details, number_of_answered_questions
```

{% endcode %}

</details>

<details>

<summary>List users who answered with a specific choice</summary>

List users who answered with the choice labeled as `always` for the question with the `how_often` NQL ID for the campaign with the `#my_campaign` NQL ID.

{% code lineNumbers="true" %}

```
campaign.#my_campaign.responses during past 30d
| where answers.how_often.label = "always"
| list user.name, time
```

{% endcode %}

</details>

<details>

<summary>List users with their last answer to a sentiment campaign</summary>

List all users with their last answers to the question with the `sentiment` question ID for the sentiment campaign with the `#service_sentiment` NQL ID (if any).

{% code lineNumbers="true" %}

```
users
| include campaign.#service_sentiment.responses
| where state == answered
| compute last_satisfaction = answers.sentiment.label.last(), last_answer_on_device = device.name.last()
| list user.name, last_satisfaction, last_answer_on_device
```

{% endcode %}

</details>

<details>

<summary>List of users with their last answer to a parametric campaign where the parameter had a specific value</summary>

List users with their last answer and comment to the question with the `sentiment` question ID and the `ticket_number` parameter associated with the request for the parametric campaign with the `#ticket_satisfaction` NQL ID, where the parameter value with the `issue_type` ID is `Incident`.

{% code lineNumbers="true" %}

```
users
| with campaign.#ticket_satisfaction.responses
| where state == answered and parameters.issue_type == "Incident"
| compute last_satisfaction = answers.sentiment.label.last(), last_comment = answers.sentiment.comment.last(), ticket_number = parameters.ticket_number.last()
| list ticket_number, last_satisfaction, last_comment, user.name
```

{% endcode %}

</details>
