# Lead Attribution

* [How Lead Attribution Works](#how-lead-attribution-works)
* [Installing the Script](#how-to-install-the-storylane-javascript-code)
* [Lead Attribution Tracking](#lead-attribution-tracking)
* [Best practices](#best-practices)

***

## 👉 How Lead Attribution Works

1. **First Visit: Unknown Lead**\
   A visitor lands on your site but hasn’t shared their email: they’re still anonymous.
2. **Demo Time: Staying Anonymous**\
   The visitor explores a Storylane demo — we identify account-level data but without the visitor submitting a form, they're still unknown at a user-level.
3. **Form Filled: They’re In**\
   The visitor eventually fills out a form on the website (with Storylane JS embeded). Now, Storylane can link their email to past demo interactions.
4. **Lead Linking Magic**\
   Boom! Storylane matches the email with previous demo actions, giving us a full picture of their journey and interest level (+ all future interactions)!

{% hint style="danger" %}
For automatic lead attribution to work, Storylane JS must be installed on *every page* of the website. Without this, we can’t detect those form submissions or link demo activity back to leads.
{% endhint %}

***

## 👉 How to Install the Storylane JavaScript Code

Add this code snippet to the `<head>` section on every page to power up lead tracking:

```html
<script>
  (function() {
    "use strict";
    function e(t) {
      var r = t.head;
      if (!r) return;
      var i = t.querySelector('script[src="https://js.storylane.io/js/v2/storylane.js"]'),
          n = t.querySelector('script[src="https://js.storylane.io/js/v1/storylane.js"]');
      if (i || n) return;
      var s = t.createElement("script");
      s.type = "text/javascript", s.async = !0, s.src = "https://js.storylane.io/js/v2/storylane.js",
      r.appendChild(s);
    }
    e(window.document);
  })();
</script>
```

***

### **👉 Lead Attribution Tracking**

### How to Set Up Using Google Tag Manager (GTM)

Add Storylane SDK quickly using Google Tag Manager without any engineering effort.

1. Sign in to [Google Tag Manager](https://tagmanager.google.com/) and select “Workspace”.
2. Click on “Add a new tag” and name it "**Storylane Tag"**.
3. Click **Edit** on Tag Configuration and under custom, select **Custom HTML**.
4. Copy the tracking script below and paste it into the HTML field. Hit **Save**.

{% code overflow="wrap" fullWidth="false" %}

```javascript
<script>(function(){"use strict";function e(t){var r=t.head;if(!r)return;var i=t.querySelector('script[src="https://js.storylane.io/js/v2/storylane.js"]'),n=t.querySelector('script[src="https://js.storylane.io/js/v1/storylane.js"]');if(i||n)return;var s=t.createElement("script");s.type="text/javascript",s.async=!0,s.src="https://js.storylane.io/js/v2/storylane.js",r.appendChild(s)}e(window.document)})();</script>
```

{% endcode %}

5. In the Triggers popup, click **Add Trigger** and select **All Pages**.
6. Once the trigger has been added, click on **Publish** at the top of your GTM window and that’s it!

***

## 👉 Best Practices

To keep everything smooth:

* [x] **Storylane JS Code:** Must be on *every page*, in the `<head>` tag.
* [x] **CRM Integrations:** We support Marketo, HubSpot, and Pardot (other CRMs coming soon).

***

{% hint style="info" %}
If you need help or have any questions, feel free to contact us at <support@storylane.io>.
{% 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://docs.storylane.io/analytics-and-performance/lead-attribution.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.
