# 如何设置事件

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

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

*   触发基于行为的消息；
*   在 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 会在第一次 `postEvent` 调用时，如果事件名称无法识别，则会自动创建该事件及其属性，并根据接收到的值推断每个属性的类型。如果您需要控制这些类型而不是让系统推断，请先声明事件及其属性类型。有关类型规则、属性数量限制以及事件名称的匹配方式，请参阅[自动创建的事件](/zh/product/audience-data-and-segmentation/events#events-created-automatically)。

您可以让您的营销团队直接在 [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>