# วิธีการตั้งค่า event

**Event** ใช้เพื่อติดตามการกระทำต่างๆ ที่ผู้ใช้ทำในแอป ข้อมูลจาก event จะถูกจัดเก็บตามลำดับเวลา สร้างไทม์ไลน์ของพฤติกรรมผู้ใช้ ข้อมูลบริบททั้งหมดเกี่ยวกับ event สามารถส่งผ่านเป็นชุดของ attribute และค่าของมันได้

เมื่อรวบรวมข้อมูลอย่างถูกต้องแล้ว ข้อมูลนี้สามารถนำไปใช้เพื่อ:

*   กระตุ้นข้อความตามพฤติกรรม
*   แก้ไขโฟลว์การสื่อสารกับผู้ใช้ภายใน Customer Journey ตามพฤติกรรมของพวกเขา
*   สร้าง segment ของผู้ใช้ที่ดำเนินการบางอย่างในแอป
*   รับข้อมูลเชิงลึกเกี่ยวกับโฟลว์ของผู้ใช้ ตัวชี้วัดการใช้งาน และข้อมูลทางสถิติอื่นๆ

## ประเภทของ event

ใน Pushwoosh มี event อยู่สองประเภท:
- Default event
- Custom event

### Default event
Default Event คือการโต้ตอบพื้นฐานที่ผู้ใช้ทำในแอปหรือเว็บไซต์ โดยไม่คำนึงถึงอุตสาหกรรมหรือฟังก์ชันการทำงาน การกระทำหลักของผู้ใช้เหล่านี้เป็นแกนหลักของการสื่อสารกับลูกค้า และสามารถนำไปใช้กับผลิตภัณฑ์และลูกค้าใดๆ ในทุกขั้นตอนของวงจรชีวิตลูกค้า
Default Event พร้อมใช้งานทันทีกับ SDK เวอร์ชันล่าสุดและไม่ต้องการการตั้งค่าเพิ่มเติมใดๆ ยกเว้น [PW_InAppPurchase](/th/product/audience-data-and-segmentation/events/default-events#pw_inapppurchase)

เรียนรู้เพิ่มเติมเกี่ยวกับ [Default Event](/th/product/audience-data-and-segmentation/events/default-events)

### Custom event
ต่างจาก default event ซึ่งเป็นสากลสำหรับแอปและภาคส่วนต่างๆ มากมาย custom event คือ event ที่คุณสร้างขึ้นสำหรับแอปของคุณโดยเฉพาะ event เหล่านี้ติดตามการกระทำเฉพาะที่ตรงกับความต้องการของคุณ เช่น การออกกำลังกายเสร็จสิ้น หรือการต่ออายุการสมัครสมาชิก และช่วยให้คุณเข้าใจว่าผู้ใช้โต้ตอบกับฟีเจอร์เฉพาะของคุณอย่างไร

Custom Event กำหนดให้คุณต้อง implement ด้วยตัวเอง


## การ implement Custom event

### 1. สร้าง event และตั้งค่า attribute

Event ทั้งหมดที่ส่งโดยแอปของคุณจะต้องถูกสร้างขึ้นใน Pushwoosh ก่อน พร้อมกับชุดของ attribute และประเภทของมัน มิฉะนั้น Pushwoosh จะไม่รู้จัก event เหล่านั้น

คุณสามารถขอให้ทีมการตลาดของคุณสร้าง event โดยตรงใน [Pushwoosh Control Panel](/th/product/audience-data-and-segmentation/events#1-create-events-in-pushwoosh-control-panel) หรือใช้วิธีการของ API [createEvent](/th/developer/api-reference/events#createevent) ซึ่งเกี่ยวข้องกับการระบุชื่อ event และข้อมูลที่เกี่ยวข้องที่คุณต้องการติดตาม

### 2. เรียกใช้ /postEvent API

เมื่อ event ที่คุณต้องการติดตามเกิดขึ้นในแอปของคุณ ให้เรียกใช้ API [`/postEvent`](/th/developer/api-reference/user-centric-api#postevent) เพื่อส่ง event นี้ไปยัง Pushwoosh

#### iOS

เรียกใช้ [`postEvent`](/th/developer/api-reference/user-centric-api#postevent) เพื่อส่ง event ไปยัง Pushwoosh:

<Tabs>
<TabItem label="Swift">
```swift
PWInAppManager.shared().postEvent("eventName", withAttributes: nil)
```
</TabItem>

<TabItem label="Objective-C">
```objective-c
[[PushNotificationManager pushManager] postEvent:@“eventName” withAttributes:@{}];
```
</TabItem>
</Tabs>

หากต้องการเพิ่มรายละเอียดเกี่ยวกับ event (อ้างอิงถึง Attribute) ให้ใช้พารามิเตอร์ attribute ดังนี้:

<Tabs>
<TabItem label="Swift">
```swift
let attributes: [String : Any] = ["AttributedString" : "someString",
                                  "AttributeInt" : 42,
                                  "AttributeList" : [123, 456, "someString"],
                                  "AttributeBool" : true,
                                  "AttributeDate" : NSDate()]

PWInAppManager.shared().postEvent("eventName", withAttributes: attributes)
```
</TabItem>

<TabItem label="Objective-C">
```objective-c
NSDictionary *attributes = @{ 
  @"AttributeString" : @"someString",
  @"AttributeInt" : @(42),
  @"AttributeList" : @[ @(123), @(456), @"someString" ],
  @"AttributeBool" : @YES,
  @"AttributeDate" : [NSDate date] 
};

[[PushNotificationManager pushManager] postEvent:@“eventName” withAttributes:attributes];
```
</TabItem>
</Tabs>

#### Android

เมื่อมี event เกิดขึ้นในแอป Android ของคุณ ให้ใช้คำแนะนำต่อไปนี้เพื่อส่ง event นี้ไปยัง Pushwoosh

เรียกใช้ [`postEvent`](/th/developer/api-reference/user-centric-api#postevent) เพื่อส่ง event ไปยัง Pushwoosh:

```java
PushwooshInApp.getInstance().postEvent("eventName");
```

หากต้องการเพิ่มรายละเอียดเกี่ยวกับ event (อ้างอิงถึง Attribute) ให้ใช้พารามิเตอร์ attribute ดังนี้:

```java
TagsBundle attributes = new TagsBundle.Builder()
				.putInt("AttributeInt", 17)
				.putString("AttributeString", "str")
				.putDate("AttributeDate", new Date())
				.putBoolean("AttributeBool", true)
				.putList("AttributeList", Arrays.asList("item1", "item2", "item3"))
				.build();

PushwooshInApp.getInstance().postEvent("eventName", attributes);
```

<Aside type="note">
คุณยังสามารถส่ง event จาก [**In-App Javascript**](/th/developer/guides/messaging-channels/inapp-with-javascript/) ได้อีกด้วย
</Aside>