# Server Actions

Tracking server actions with PluginPulse is as easy as tracking them on the client. The main difference is how you load the PluginPulse tracking code into your plugin.

### Importing PluginPulse for server actions

Instead of a script snippet in the header, PluginPulse is loaded into a server action via an [npm package](https://www.npmjs.com/package/@jakebennett18505/pluginpulse) in the package.json section in your server action (found at the bottom of the page).

{% stepper %}
{% step %}

### Add npm package

{% code title="package.json" %}

```json
{
    "dependencies": {
        "pluginpulse": "latest"
    }
}
```

{% endcode %}
{% endstep %}

{% step %}

### Rebuild the package.json

<figure><img src="/files/NMfjteHLzK8h00bkdI7E" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### Import into action

Note: The server action is synchronous by default. To use PluginPulse, convert your function to async.

```javascript
async function(properties, context) {
    const { pluginpulse } = require("pluginpulse");
}
```

{% endstep %}
{% endstepper %}

### Sending events in server actions

The overall structure of the event remains very similar. The only change is in order to identify the application, you need to pass the context from the parent function along with your plugin id and payload.

### Parameters

<table><thead><tr><th width="124.58984375">Parameter</th><th width="100.01953125">Required</th><th>Description</th><th>Example</th></tr></thead><tbody><tr><td>plugin_id</td><td>Yes</td><td>Your PluginPulse plugin ID</td><td>"1706526741286x876166435619471400"</td></tr><tr><td>context</td><td>Yes</td><td>The context of the action that includes the app identifier</td><td>-</td></tr><tr><td>event_name</td><td>No</td><td>Name of the event/feature (default: "Server Action")</td><td>"Export PDF"</td></tr><tr><td>payload</td><td>No</td><td>Custom JSON data for additional context</td><td><code>{format: "pdf", pages: 5}</code></td></tr></tbody></table>

#### Basic

When passing no information, the below code will result in a PluginPulse event with the name "Server Action"

```javascript
async function(properties, context) {
    const { pluginpulse } = require("pluginpulse");

    // Your existing code...

    await pluginpulse("your-plugin-id", context);
}
```

#### Custom data

As with client side actions, you can define the data you want to pass.

```javascript
async function(properties, context) {
  const { pluginpulse } = require("pluginpulse");
  
  // Your existing code...
  
  await pluginpulse("your-plugin-id", context, {
    event_name: "PDF Generated",
    payload: {
      size: "A4",
    },
  });
}

```

{% hint style="warning" %}
**The plugin version isn't available in the server action context, so all server events will be sent with the version as `backend`**
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://plugin-pulse.gitbook.io/docs/server-actions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
