# 如何设置事件

**事件**用于跟踪用户在应用中执行的各种操作。事件数据会按时间顺序存储，从而创建用户行为的时间线。所有关于事件的上下文信息都可以作为一组属性及其值来传递。

一旦正确收集，这些数据可用于：

*   触发基于行为的消息；
*   根据用户行为在 Customer Journey 中修改用户沟通流程；
*   构建在应用中执行特定操作的用户分群；
*   获取关于用户流程、使用指标和其他统计数据的洞察。

## 事件类型

Pushwoosh 中有两种类型的事件：
- 默认事件
- 自定义事件

### 默认事件
默认事件是用户在应用或网站中执行的基本交互，无论其行业或功能如何。这些关键用户行为构成了客户沟通的核心，并且可以在客户生命周期的任何阶段为任何产品和客户所用。
最新版本的 SDK 中已内置默认事件，除 [PW_InAppPurchase](/zh/product/audience-data-and-segmentation/events/default-events#pw_inapppurchase) 外，无需任何额外设置。

了解更多关于[默认事件](/zh/product/audience-data-and-segmentation/events/default-events)

### 自定义事件
与跨多个应用和行业通用的默认事件不同，自定义事件是您专门为您的应用创建的事件。这些事件跟踪您特定需求的独特操作，例如完成一次锻炼或延长订阅，并帮助您了解用户如何与您的特定功能互动。

自定义事件需要您在自己的端实现。


## 自定义事件实现

### 1. 创建事件并设置属性

您的应用发送的所有事件必须首先在 Pushwoosh 中创建，并设置好属性及其类型；否则，Pushwoosh 将无法识别它们。

您可以让您的营销团队直接在 [Pushwoosh 控制面板](/zh/product/audience-data-and-segmentation/events#1-create-events-in-pushwoosh-control-panel)中创建事件，或使用 [createEvent](/zh/developer/api-reference/events#createevent) API 方法。这包括指定事件名称和您想要跟踪的任何相关数据。

### 2. 调用 /postEvent API

当您想要跟踪的事件在您的应用中发生时，调用 [`/postEvent`](/zh/developer/api-reference/user-centric-api#postevent) API 将此事件发送到 Pushwoosh。

#### iOS

调用 [`postEvent`](/zh/developer/api-reference/user-centric-api#postevent) 将事件发送到 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>

要添加关于事件的详细信息（参考属性），请按如下方式使用 attributes 参数：

<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

当事件在您的 Android 应用中发生时，请使用以下说明将此事件发送到 Pushwoosh。

调用 [`postEvent`](/zh/developer/api-reference/user-centric-api#postevent) 将事件发送到 Pushwoosh：

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

要添加关于事件的详细信息（参考属性），请按如下方式使用 attributes 参数：

```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">
您也可以通过[**应用内 Javascript**](/zh/developer/guides/messaging-channels/inapp-with-javascript/) 发送事件。
</Aside>