---
title: Source Code Context
description: >-
  Display source code inline in Error Tracking stack traces by injecting service
  and version metadata at build time.
breadcrumbs: >-
  Docs > RUM & Session Replay > Application Monitoring > RUM Browser Monitoring
  > Build Plugins > Source Code Context
---

# Source Code Context

## Overview{% #overview %}

When viewing errors in [Error Tracking](https://docs.datadoghq.com/real_user_monitoring/error_tracking.md), Datadog can display the source code lines surrounding each frame in the stack trace. The Source Code Context build plugin enables this feature by injecting a small runtime snippet into your bundle that associates stack traces with your `service` and `version` metadata.

At build time, the plugin injects a snippet that writes metadata to `window.DD_SOURCE_CODE_CONTEXT`. At runtime, the RUM SDK reads `window.DD_SOURCE_CODE_CONTEXT` to tag errors with the correct service and version for source code resolution. This works in conjunction with [uploaded source maps](https://docs.datadoghq.com/real_user_monitoring/guide/upload-javascript-source-maps.md) — source maps provide the file mapping, and `window.DD_SOURCE_CODE_CONTEXT` provides the service and version association.

## Prerequisites{% #prerequisites %}

- Source maps uploaded to Datadog, either through the [Source Maps build plugin](https://docs.datadoghq.com/real_user_monitoring/application_monitoring/browser/build_plugins/source_maps.md) or [manually](https://docs.datadoghq.com/real_user_monitoring/guide/upload-javascript-source-maps.md).
- The RUM SDK initialized with matching `service` and `version` parameters.
- The Datadog build plugin installed and registered with your bundler. See [Build Plugins](https://docs.datadoghq.com/real_user_monitoring/application_monitoring/browser/build_plugins.md) for installation instructions.

## Configuration{% #configuration %}

Configure the `rum.sourceCodeContext` object in your build plugin options:

| Parameter                       | Type   | Required | Default | Description                                                                                                                                                    |
| ------------------------------- | ------ | -------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `rum.sourceCodeContext.service` | String | Yes      | None    | Service name. Must match the RUM SDK `service` initialization parameter.                                                                                       |
| `rum.sourceCodeContext.version` | String | No       | None    | Release version. If omitted, source code context is not associated with a specific version. If set, must match the RUM SDK `version` initialization parameter. |

## Example{% #example %}

The following example shows source code context combined with source maps, a common pairing:

```javascript
const { datadogWebpackPlugin } = require('@datadog/webpack-plugin');

module.exports = {
  plugins: [
    datadogWebpackPlugin({
      auth: {
        apiKey: process.env.DATADOG_API_KEY,
      },
      errorTracking: {
        sourcemaps: {
          service: 'my-application',
          releaseVersion: '1.0.0',
          minifiedPathPrefix: 'https://example.com/static/',
        },
      },
      rum: {
        sourceCodeContext: {
          service: 'my-application',
          version: '1.0.0',
        },
      },
    }),
  ],
};
```

{% alert level="info" %}
This example uses webpack. The configuration object is identical across all supported bundlers. See [Build Plugins](https://docs.datadoghq.com/real_user_monitoring/application_monitoring/browser/build_plugins.md) for installation instructions for your bundler.
{% /alert %}

## Further reading{% #further-reading %}

- [Upload JavaScript Source Maps](https://docs.datadoghq.com/real_user_monitoring/guide/upload-javascript-source-maps.md)
- [Error Tracking](https://docs.datadoghq.com/real_user_monitoring/error_tracking.md)
- [Debug Symbols](https://docs.datadoghq.com/real_user_monitoring/guide/debug-symbols.md)
- [Datadog Build Plugins GitHub Repository](https://github.com/DataDog/build-plugins)
