---
title: "Configure Sampling"
description: "Learn how to configure sampling in your app using the Sentry JavaScript SDK."
url: https://docs.sentry.io/platforms/javascript/guides/cordova/tracing/configure-sampling/
---

# Configure Sampling for Cordova

Sentry's tracing functionality helps you monitor application performance by capturing distributed traces, attaching attributes, and span performance across your application. However, Capturing traces for every transaction can generate significant volumes of data. Sampling allows you to control the amount of spans that are sent to Sentry from your application.

The JavaScript SDK provides two main options for controlling the sampling rate:

1. [Uniform Sample Rate](https://docs.sentry.io/platforms/javascript/guides/cordova/tracing/configure-sampling.md#uniform-sample-rate-tracessamplerate) (recommended)
2. [Sampling Function](https://docs.sentry.io/platforms/javascript/guides/cordova/tracing/configure-sampling.md#sampling-function-tracessampler)

## [Uniform Sample Rate (`tracesSampleRate`)](https://docs.sentry.io/platforms/javascript/guides/cordova/tracing/configure-sampling.md#uniform-sample-rate-tracessamplerate)

`tracesSampleRate` is floating point value 0.0 and 1.0, which controls the probability that a transaction will be sampled.

```javascript
Sentry.init({
  dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
  integrations: [Sentry.browserTracingIntegration()],
  // Capture 25% of all transactions
  tracesSampleRate: 0.25,
});
```

With `tracesSampleRate` set to `0.25`, each transaction in your application is randomly sampled with a probability of 25%, so you can expect that one in every four transactions will be sent to Sentry.

## [Sampling Function (`tracesSampler`)](https://docs.sentry.io/platforms/javascript/guides/cordova/tracing/configure-sampling.md#sampling-function-tracessampler)

For more granular control, you provide a `tracesSampler` function. This approach allows you to:

* Apply different sampling rates to different types of transactions
* Filter out specific transactions entirely
* Make sampling decisions based on transaction data
* Control the inheritance of sampling decisions in distributed traces

```javascript
Sentry.init({
  dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
  integrations: [Sentry.browserTracingIntegration()],
  tracesSampler: (samplingContext) => {
    // Access transaction details from the sampling context
    const { transactionContext, parentSampled } = samplingContext;

    // Skip health checks entirely
    if (transactionContext.name.includes("healthcheck")) {
      return 0;
    }

    // Capture all auth-related transactions
    if (transactionContext.name.includes("auth")) {
      return 1;
    }

    // Sample only 1% of comment-related transactions
    if (transactionContext.name.includes("comment")) {
      return 0.01;
    }

    // For everything else, inherit the parent sampling decision or use 0.5
    return parentSampled !== undefined ? parentSampled : 0.5;
  },
});
```

### [The Sampling Context Object](https://docs.sentry.io/platforms/javascript/guides/cordova/tracing/configure-sampling.md#the-sampling-context-object)

When the `tracesSampler` function is called, it receives a `samplingContext` object with valuable information to help make sampling decisions:

```typescript
interface SamplingContext {
  // Context of the transaction, including its initial `name` and `op`
  transactionContext: TransactionContext;

  // Whether the parent transaction was sampled (undefined if there's no parent)
  parentSampled: boolean | undefined;

  // The current `window.location`
  location: Location | undefined;
}
```

### [Traces Sampler Examples](https://docs.sentry.io/platforms/javascript/guides/cordova/tracing/configure-sampling.md#traces-sampler-examples)

1. Prioritizing Critical User Flows

```javascript
tracesSampler: (samplingContext) => {
  const { transactionContext, parentSampled } = samplingContext;

  // Sample all checkout transactions
  if (transactionContext.name.includes("/checkout")) {
    return 1.0;
  }

  // Sample 50% of login transactions
  if (transactionContext.name.includes("/login")) {
    return 0.5;
  }

  // Sample 10% of everything else, unless there's a parent sampling decision
  return parentSampled !== undefined ? parentSampled : 0.1;
};
```

2. Handling Different Environments

```javascript
tracesSampler: (samplingContext) => {
  // Sample all transactions in development
  if (process.env.NODE_ENV === "development") {
    return 1.0;
  }

  // Sample 5% in production
  if (process.env.NODE_ENV === "production") {
    return 0.05;
  }

  // Sample 20% in staging
  return 0.2;
};
```

## [Sampling Decision Precedence](https://docs.sentry.io/platforms/javascript/guides/cordova/tracing/configure-sampling.md#sampling-decision-precedence)

When multiple sampling mechanisms could apply, Sentry follows this order of precedence:

* If `tracesSampler` is defined, its decision is used. Although the `tracesSampler` can override the parent sampling decision, most users will want to ensure their `tracesSampler` respects the parent sampling decision.
* If no `tracesSampler` is defined, but there is a parent sampling decision from an incoming distributed trace, we use the parent sampling decision
* If neither of the above, `tracesSampleRate` is used
* If `tracesSampleRate` is set to 0, no spans will be sampled, and no downstream spans will be sampled either since they inherit the parent sampling decision
* If none of the above are set, no transactions are sampled and tracing will be disabled

## [Conclusion](https://docs.sentry.io/platforms/javascript/guides/cordova/tracing/configure-sampling.md#conclusion)

Effective sampling is key to getting the most value from Sentry's performance monitoring while minimizing overhead. The `tracesSampler` function gives you precise control over which transactions to record, allowing you to focus on the most important parts of your application.
