# Direct JS Implementation

This guide focuses on the direct JS implementation method for integrating the Antsomi SDK into your web application. By manually placing the tracking script within your website's code, you gain complete control over its placement and timing.

## **Retrieve Your Tracking Script** <a href="#retrieve-your-tracking-script" id="retrieve-your-tracking-script"></a>

To begin, follow these steps to retrieve the required script from your CDP365 account:

### **1. Access Your CDP365 Dashboard** <a href="#id-1.-access-your-cdp365-dashboard" id="id-1.-access-your-cdp365-dashboard"></a>

* Navigate to your CDP365 dashboard.

<figure><img src="https://2980605795-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi16t4WMYZYowEFalUZLL%2Fuploads%2FD0cKcRe7aeRxST4KrMkr%2Fimage.png?alt=media&#x26;token=2fa85475-2be1-4269-8103-8b8fbdc959b6" alt=""><figcaption><p>CDP365 dashboard</p></figcaption></figure>

### **2. Locate Event Sources** <a href="#id-2.-locate-event-sources" id="id-2.-locate-event-sources"></a>

* Click the hamburger menu icon in the top left corner.

<figure><img src="https://2980605795-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi16t4WMYZYowEFalUZLL%2Fuploads%2F2C2U9wmrHsciffVyegsr%2Fimage.png?alt=media&#x26;token=6a52c4ed-8857-41ce-8640-e39294e85654" alt=""><figcaption><p>The burger button on the top left to open the menu sidebar</p></figcaption></figure>

* Select *'Data Hub'* > *'Event Sources'* from the menu.

<figure><img src="https://2980605795-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi16t4WMYZYowEFalUZLL%2Fuploads%2FIJUoGhwRevcpR94kwDKY%2Fimage.png?alt=media&#x26;token=ea3fb5fe-c8a0-403f-b99e-aa333b5cc80c" alt=""><figcaption><p>The expanded menu Data Hub and its submenu Event Sources</p></figcaption></figure>

### **3. Choose Your Script** <a href="#id-3.-choose-your-script" id="id-3.-choose-your-script"></a>

* From the available options, select your desired source, typically *'Website'* for web app integrations.

<figure><img src="https://2980605795-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi16t4WMYZYowEFalUZLL%2Fuploads%2FFW3U1s0KjZtSuCcQBtbl%2Fimage.png?alt=media&#x26;token=6202092c-8629-4186-90c6-e09520e4166e" alt=""><figcaption><p>Your desired source (currently it is the source <em>'Website'</em>)</p></figcaption></figure>

### **4. Download the Script** <a href="#id-4.-download-the-script" id="id-4.-download-the-script"></a>

* Navigate to the "Settings" tab.
* Locate the "Main Tracking Script" section.

<figure><img src="https://2980605795-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi16t4WMYZYowEFalUZLL%2Fuploads%2FR6XIJfwzt8BLS89o6bS7%2Fimage.png?alt=media&#x26;token=c2b9c0b2-6a33-4bca-986c-9d024f467c1d" alt=""><figcaption><p>Tab Event Setting</p></figcaption></figure>

* Copy the provided script code, ensuring you capture the entire section within the `<script>` tags.

{% tabs %}
{% tab title="HTML" %}

```html
<!-- CDP Web Insight script -->
<script type = "text/javascript" >
   var _portalId = "564890637"; // Portal Demo Ecom 
   var _propId = "564993250"; 
   var _ATM_TRACKING_ASSOCIATE_UTM = 0 ; // https://demo-ecom.antsomi.com 
(function() {
    var w = window;
    if (w.web_event) return;
    var a = window.web_event = function() {
        a.queue.push(arguments);
    }
    a.propId = _propId;
    a.track = a;
    a.queue = [];
    var e = document.createElement("script");
    e.type = "text/javascript", e.async = !0, e.src = "//st-a.cdp.asia/insight.js";
    var t = document.getElementsByTagName("script")[0];
    t.parentNode.insertBefore(e, t)
})(); </script>
<!-- End of CDP Web Insight script -->
```

{% endtab %}
{% endtabs %}

Since the default main script only serves websites with 1 domain, if you have multiple sub-domains that you want to track your users' behavior, please adjust the main script as follows:

{% tabs %}
{% tab title="HTML" %}

```markup
<!-- CDP Web Insight script -->
<script type = "text/javascript" >
   var _portalId = "564890637"; // Portal Demo Ecom 
   var _propId = "564993250"; 
   var _ATM_TRACKING_ASSOCIATE_UTM = 0 ; // https://demo-ecom.antsomi.com 
   
   // Add this declaration
   var _cdp365Analytics = {
       first_party_domain: ".<DOMAIN>" // e.g.: .antsomi.com
   };
   
(function() {
    var w = window;
    if (w.web_event) return;
    var a = window.web_event = function() {
        a.queue.push(arguments);
    }
    a.propId = _propId;
    a.track = a;
    a.queue = [];
    var e = document.createElement("script");
    e.type = "text/javascript", e.async = !0, e.src = "//st-a.cdp.asia/insight.js";
    var t = document.getElementsByTagName("script")[0];
    t.parentNode.insertBefore(e, t)
})(); </script>
<!-- End of CDP Web Insight script -->
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
Please replace the <mark style="color:blue;">`".<DOMAIN>"`</mark> with your real domain, and pay attention to the dot (.) at the beginning too.
{% endhint %}

## **Implement the Script** <a href="#implement-the-script" id="implement-the-script"></a>

Now, it's time to place the script within your web app's code. Follow these steps for successful implementation:

### **1. Identify Tracked Pages:** <a href="#id-1.-identify-tracked-pages" id="id-1.-identify-tracked-pages"></a>

* Determine which pages you want to track user behavior on within your web app.

### **2. Add the Script:** <a href="#id-2.-add-the-script" id="id-2.-add-the-script"></a>

* Open the HTML files for your chosen pages.
* Locate the `<head>` tag at the beginning of the document.
* Paste the copied script code **immediately inside the opening `<head>` tag**.

Ensure the script placement is consistent across all desired pages for accurate data collection.

## **Custom Event Tracking with Antsomi JS Functions** <a href="#custom-event-tracking-with-antsomi-js-functions" id="custom-event-tracking-with-antsomi-js-functions"></a>

### Prepare the Event Data

Before sending your first event data, you must assign that event to the desired source (in Website tracking, the source is usually named *'Website'*). Then you can have the event firing code snippet be the following:

* From your source, identify your tracking event (in this example is the event *'View Product'*):

<figure><img src="https://2980605795-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi16t4WMYZYowEFalUZLL%2Fuploads%2FFW3U1s0KjZtSuCcQBtbl%2Fimage.png?alt=media&#x26;token=6202092c-8629-4186-90c6-e09520e4166e" alt=""><figcaption><p>Your desired source (currently it is the source "Website")</p></figcaption></figure>

* Hover to its name, a small button will appear

<figure><img src="https://1976329911-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi16t4WMYZYowEFalUZLL%2Fuploads%2FwS3YpQ0HIvgnN3NvCgBG%2Fimage.png?alt=media&#x26;token=083714a1-ffbc-4fa2-8d31-536c640522fd" alt=""><figcaption><p>Your tracking event (in this example is the event <em>'View Product'</em>)</p></figcaption></figure>

* Click to the button and you will have your prepared code snippet

<figure><img src="https://1976329911-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi16t4WMYZYowEFalUZLL%2Fuploads%2FUBQJAlYdpsleboDUFGBt%2Fimage.png?alt=media&#x26;token=de64c69c-a688-4ad5-be9e-f2ecc2402787" alt=""><figcaption><p>Your event script</p></figcaption></figure>

### Send your Event

After you have had your event script code snippet, you can start putting the data into these placeholders.

#### **1. Check for Script Loading** <a href="#id-1.-check-for-script-loading" id="id-1.-check-for-script-loading"></a>

* Before calling `web_event.track()`, ensure the main tracking script is fully loaded. You can achieve this by verifying the existence of the function using a conditional statement:

{% tabs %}
{% tab title="JavaScript" %}

```javascript
if (typeof web_event.track === 'function') {
  // Proceed with calling web_event.track()
} else {
  // Handle the case where the script hasn't loaded yet
}
```

{% endtab %}
{% endtabs %}

#### **2. Structure Your Event** <a href="#id-2.-structure-your-event" id="id-2.-structure-your-event"></a>

* The `web_event.track()` function accepts three parameters:
  * **Event Category:** A general classification of the event (e.g., *'product'*, *'pageview'*).
  * **Event Action:** A specific action within the category (e.g., *'view'*, *'add\_to\_cart'*).
  * **Event Data:** An object containing details about the event, including:
    * `items`**:** An array of objects representing the [**Main Object**](https://docs.antsomi.com/developer-guide/~/changes/ozJy59lgDpdkZGSZKOYm/overview/events#main-object-vs.-foreign-objects-defining-the-event-context) of the event. This will be mapped to the primary Business Object in CDP365.
    * `dims`**:** An object containing [**Foreign Objects**](https://docs.antsomi.com/developer-guide/~/changes/ozJy59lgDpdkZGSZKOYm/overview/events#main-object-vs.-foreign-objects-defining-the-event-context), providing additional context for the event. These will be mapped to related Business Objects in CDP365.
    * `extra`**:** An object for [**Event Attributes**](https://docs.antsomi.com/developer-guide/~/changes/ozJy59lgDpdkZGSZKOYm/overview/events#event-attributes), capturing specific details about the event's circumstances. These will be added as attributes to the main event object in CDP365.

**Example: Product View Event**

<figure><img src="https://1976329911-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fi16t4WMYZYowEFalUZLL%2Fuploads%2Fh46hAfRzmhqdstIUjMmD%2Fimage.png?alt=media&#x26;token=bb2e0699-78bd-4152-883f-0c3596508a5a" alt=""><figcaption><p>Event <em>'View Product'</em> settings on the CDP365 dashboard</p></figcaption></figure>

{% tabs %}
{% tab title="JavaScript" %}

<pre class="language-javascript"><code class="lang-javascript">/** start tracking event: view_product */
<strong>web_event.track("product", "view", {
</strong>  // Main Object (mapped to Product Business Object in CDP365)
  items: [
    {
      type: "product",
      id: "{{product_id}}",
      name: "{{product_name}}",
      page_url: "{{product_page_url}}",
      // ... other product details
    }
  ],
  // Foreign Objects (mapped to Customer and other relevant Business Objects in CDP365)
  dims: {
    customers: {
      customer_id: "{{customer_id}}",
      name: "{{customer_name}}",
      // ... other customer information
    }
    // ... other Foreign Objects (e.g., campaigns, devices)
  },
  // Event Attributes (added as attributes to the event object in CDP365)
  extra: {
    identify_type: "exact",
    identify_event: "{{event_action}}", // Note: This should match the event action
    identify_id: "{{customer_id}}",
    identify_time: "{{identify_time}}"
    // ... other Event Attributes
  }
});
/** end block */
</code></pre>

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
**Notes**

The placeholders like `{{product_id}}` and `{{customer_id}}`. Replace these with actual values from your web app's data to ensure accurate tracking.
{% endhint %}
