# Custom Interactions

> Discover how to log custom interactions in your AppExchange app and visualize them with ISV Analytics.

# Custom Interactions

![Custom Interactions](img/custom-interactions.png)

## Produce Your Own Logs with Custom Interactions

_Custom Interactions_ is a feature that allows you to define your own log events to track as part of app analytics.

Salesforce's [AppExchange App Analytics](https://developer.salesforce.com/docs/atlas.en-us.pkg2_dev.meta/pkg2_dev/app_analytics_intro_2gp.htm) (the data that powers the _ISV Analytics_ app) includes your app usage logs with a predefined list of log events - such as opening a Lightning page, creating a record, or receiving a REST call. (To learn more about predefined log events, refer to the [official Salesforce documentation](https://developer.salesforce.com/docs/atlas.en-us.pkg2_dev.meta/pkg2_dev/app_analytics_read_logs_intro.htm).)

Custom Interactions enable you to go beyond that predefined list and track critical events in your app's lifecycle, such as:

- A user completes a business process with success or failure.
- A user reaches a milestone, indicating they have begun to derive real value from your app (have you heard of the [_aha moment_](https://en.wiktionary.org/wiki/aha_moment) concept?).
- An automated process is launched for the _n-th_ time and exceeds a specific threshold.
- A caught exception occurs - your app might have a fallback route, but you'd likely want to know if it happens frequently.
- A user completes the end-to-end path of a process - you can log custom interactions at each stage to understand the detailed customer journey.

## How to Use the Custom Interactions Feature

### Log Interactions in Apex

Here's an example of how you can use custom interactions to log caught exceptions. We implement this similarly in the _ISV Analytics_ app ;)

1. First, define an Enum (or multiple Enums) for your custom interaction types.

   ```apex
   public class CustomInteractionTypes {
     public enum AppCaughtExceptions {
       AI_API_ERROR,
       ORDER_CANCELLATION_FAILED,
       ORDER_RENEWAL_FAILED
     }
   }
   ```

2. Then, call the `IsvPartners.AppAnalytics.logCustomInteraction` method from your Apex code, passing the appropriate enum as an argument.

   ```apex
   public inherited sharing class AiApi {
     public void doSomethingClever() {
       try {
         // Do something clever
       } catch (Exception e) {
         IsvPartners.AppAnalytics.logCustomInteraction(
           CustomInteractionTypes.AppCaughtExceptions.AI_API_ERROR
         );
         // ... your other fallback logic
       }
     }
   }
   ```

3. Voilà! When you include this change in the new package version, Salesforce will automatically start collecting your custom interaction logs. You will be able to see them in the _ISV Analytics_ dashboards.

For more details about logging custom interactions, refer to the Salesforce documentation in the [Resources](#resources) section below.

### Observe Your Interactions in ISV Analytics

The custom interactions logged by your package are visualized on the _Custom Interactions_ tab in _ISV Analytics_.
Open the `ISV Analytics` app from the app launcher and navigate to `Custom Interactions`.

![Custom Interactions](img/custom-interactions.png)

## Resources

- [What are Custom Interactions - Salesforce Documentation](https://developer.salesforce.com/docs/atlas.en-us.pkg2_dev.meta/pkg2_dev/app_analytics_read_logs_custom_interactions.htm)
- [Custom Interactions Considerations and Usage - Salesforce Documentation](https://developer.salesforce.com/docs/atlas.en-us.pkg2_dev.meta/pkg2_dev/app_analytics_read_logs_custom_interactions.htm)
- [Data Included in Standard AppExchange App Analytics Logs - Salesforce Documentation](https://developer.salesforce.com/docs/atlas.en-us.pkg2_dev.meta/pkg2_dev/app_analytics_read_logs_intro.htm)
- [Salesforce AppExchange App Analytics Official Documentation](https://developer.salesforce.com/docs/atlas.en-us.pkg2_dev.meta/pkg2_dev/app_analytics_intro_2gp.htm)
