﻿# UTM tags in LiveChat

> [HTML Version](livechat-utm.html)

When creating a new session with a user who contacted via the [LiveChat](livechat.md), the following UTM tags are automatically captured in BRIX: 

- **utm\_source**. The source of the transition.

- **utm\_medium**. Traffic type.

- **utm\_campaign**. The name of the ad campaign.

- **utm\_content**. Additional information that helps to distinguish ads.

- **utm\_term**. Key phrase.

With these tags, you can track where the most potential customers come from and what topics are most in demand. Captured tags are displayed on the [session page](working_in_chat.md#session-page) on the right sidebar of the chat.

You can also pass tag values to a_ _[business process](chat-lines-processes.md). To do this, use [Script](360027203731.md). For example, you can set up a process where UTM tag values will be output to the [lead](leads.md) page after the lead is automatically created. Read more about scripting in the BRIX TS SDK in the [Live Chats](https://tssdk.brix365.com/en/latest/modules/_09_lines_.html) workspace.

## Example of using UTM tags in a business process 

Let’s consider an example where when creating a new session with a user from LiveChat in BRIX, a new [lead](leads.md) will be automatically created using a business process. At the same time, captured UTM tags will be displayed on the lead page.

1. Go to the [Leads](leads.md) app in the [CRM](360012584940.md) workspace.

2. Click the gear icon next to the app name and select **Business Processes**.

3. In the upper right corner, click **+Process**. In the opened window, enter the name of the process, for example, **Creating a lead from a session**.

4. In the opened business process designer, open the **Context** tab and add a variable with the following parameters:

- **Display name\***. **Lead**.

- **Property Name\***. **Opportunity**.

- **Type\***. **App**,** **One.

- **App\***. **Leads**.

5. Then create another variable: 

- **Display name\*. Session ID**.

- **Property Name\*. Session\_id**.

- **Type\*. String**.

6. Go to the **Scripts** tab and add the following code: 

````
async function createOpportunity(): Promise<void> \{  
      
    if(Context.data.session\_id) \{  
          
        const session = await System.lines.sessions.search().where(item => item.\_\_id.eq(Context.data.session\_id\!)).first();  
        if(session \&\& session.data.\_clients \&\& session.data.\_clients.length > 0) \{  
              
            const client = await System.lines.clients.search().where(item => item.\_\_id.eq(session.data.\_clients\!\[0\].id)).first();  
            if(client) \{  
  
                if(client.data.\_applicationItem) \{  
                    
                    const new\_opportunity = await Context.fields.opportunity.app.create();  
  
                    new\_opportunity.data.\_contacts = \[client.data.\_applicationItem as any\];  
                    new\_opportunity.data.utm\_source = (<any>client.data.\_extraData).utm\_source;  
                    new\_opportunity.data.utm\_medium = (<any>client.data.\_extraData).utm\_medium;  
                    new\_opportunity.data.utm\_campaign = (<any>client.data.\_extraData).utm\_campaign;  
  
                    await new\_opportunity.save();  
  
                    Context.data.opportunity = new\_opportunity;  
                \}  
            \}  
        \}  
    \}  
\}

7. ````
Go to the **Flow chart** tab and place the [Script](360027203731.md) element on the modeling field. Double-click on it and in the opened settings window in the **Function** field specify the \[OBJECT\] function created on the **Scripts** tab. Click **Save**. 

In our example, only the **Script** element is sufficient for automatic lead generation and UTM tagging, so the process flow chart can look like this:

**(utm1.png)**

8. On the top panel of the business process designer, click **Save**, and then **Publish**.

9. Bring up the properties for UTM tags in the view page of the lead. To do this, click the gear icon to the right of the **Leads** app name and select **Form** **Settings**. In the opened window, click the **View** tab and bring the properties for UTM tags located in the left panel to the form:

**(utm2.png)**

10. Save the form and go to the **Administration > Live Chat** workspace.

11. Open the **Business Processes** tab. Opposite **When a new session is created**, click **Select** and specify the previously created process **Creating a lead from a session**.

12. In the **Input parameters** column, click **Configure**. In the opened window, click **+Field** and select the **Session ID** property created in step 5. In the **Live Chats** **app field** column, specify **Session > ID**.

**(utm3.png)**

13. Click **Save**. After closing the window, save the changes made to the **Business Processes** tab.

After that, when creating a new session with a user from LiveChat, a new lead will be created in the **CRM** workspace, and its page will display the captured UTM tags.