# iOS 埋点SDK

## 埋点 SDK集成  <a href="#mai-dian-sdk-ji-cheng" id="mai-dian-sdk-ji-cheng"></a>

### 1. 选择集成方式 <a href="#id-1-xuan-ze-ji-cheng-fang-shi" id="id-1-xuan-ze-ji-cheng-fang-shi"></a>

#### 注意: 请保证Growing,GrowingCoreKit版本号一致 <a href="#zhu-yi-qing-bao-zheng-growinggrowingcorekit-ban-ben-hao-yi-zhi" id="zhu-yi-qing-bao-zheng-growinggrowingcorekit-ban-ben-hao-yi-zhi"></a>

#### （1）使用 CocoaPods 快速集成 <a href="#id-1-shi-yong-cocoapods-kuai-su-ji-cheng" id="id-1-shi-yong-cocoapods-kuai-su-ji-cheng"></a>

* 添加`pod 'GrowingCoreKit'`到 Podfile 中
* 执行`pod update`，不要用`--no-repo-update`选项
* **(optional)** GrowingIO推荐您添加**AdSupport.framework**依赖库,用于来源管理激活匹配,有利于您更好的分析数据 ,添加项目依赖库的位置在项目设置target -> 选项卡General -> Linked Frameworks and Libraries
* 直接进行第 2 步 “[设置 URL Scheme](https://docs.growingio.com/docs/sdk-integration/ios-sdk/mai-dian-sdk-ji-cheng#2-she-zhi-url-scheme)”

#### （2）手动集成 SDK  <a href="#id-2-shou-dong-ji-cheng-sdk" id="id-2-shou-dong-ji-cheng-sdk"></a>

* 下载 iOS SDK以下包：[GrowingHeader](https://assets.growingio.com/sdk/ios/GrowingIO-iOS-PublicHeader-2.8.7.zip) ，[GrowingCoreKit](https://assets.growingio.com/sdk/ios/GrowingIO-iOS-CoreKit-2.8.7.zip)​
* 解压 iOS SDK 压缩文件
* &#x20;将Growing.h,GrowingCoreKit.framework添加到iOS工程中。

{% hint style="warning" %}
&#x20;**提醒:** 记得勾选 "Copy items if needed"
{% endhint %}

添加依赖, 在项目中添加以下库文件

| **库名称**                       | **类型**                 |
| ----------------------------- | ---------------------- |
| Foundation.framework          | 基础依赖库                  |
| Security.framework            | 用于SSL连接                |
| CoreTelephony.framework       | 用于读取运营商名称              |
| SystemConfiguration.framework | 用于判断网络状态               |
| AdSupport.framework           | 用于来源管理激活匹配             |
| libicucore.tbd                | 用于WebSocket            |
| libsqlite3.tbd                | 存储日志                   |
| CoreLocation.framework        | 用于读取地理位置信息（如果您的app有权限） |
| JavaScriptCore.framework      | Web圈app交互              |
| WebKit.framework              | Web圈选                  |

{% hint style="warning" %}
提醒：添加项目依赖库的位置在项目设置target -> 选项卡General -> Linked Frameworks and Libraries
{% endhint %}

添加编译参数，注意大小写:

![](/files/-LsvxVh47pjWjhXloJQh)

### **2. 设置URL Scheme** <a href="#id-2-she-zhi-url-scheme" id="id-2-she-zhi-url-scheme"></a>

#### **(1) 获取URL Scheme** <a href="#id-1-huo-qu-url-scheme" id="id-1-huo-qu-url-scheme"></a>

• 添加新产品：登录官网-> 点击项目选择框-> 点击“项目管理” -> 点击“应用管理” -> 点击“新建应用”->选择添加iOS 应用-> 填写“应用名称“，点击下一步->在第二段中标黄字体。

• 现有产品：登录官网-> 点击项目选择框-> 点击“项目管理” -> 点击“应用管理” -> 找到对应产品的URL Scheme**设置URL Scheme**

#### **(1) 获取URL Scheme** <a href="#id-1-huo-qu-url-scheme" id="id-1-huo-qu-url-scheme"></a>

• 添加新产品：登录官网-> 点击项目选择框-> 点击“项目管理” -> 点击“应用管理” -> 点击“新建应用”->选择添加iOS 应用-> 填写“应用名称“，点击下一步->在第二段中标黄字体。

• 现有产品：登录官网-> 点击项目选择框-> 点击“项目管理” -> 点击“应用管理” -> 找到对应产品的URL Scheme

![](/files/-Lsvy7EB0a2mZK0xahQf)

#### **(2) 添加URL Scheme（growing.xxxxxxxxxxxxxxxx）到项目中** <a href="#id-2-tian-jia-url-schemegrowingxxxxxxxxxxxxxxxx-dao-xiang-mu-zhong" id="id-2-tian-jia-url-schemegrowingxxxxxxxxxxxxxxxx-dao-xiang-mu-zhong"></a>

#### **(3) 在AppDelegate 中添加代码** <a href="#id-3-zai-appdelegate-zhong-tian-jia-dai-ma" id="id-3-zai-appdelegate-zhong-tian-jia-dai-ma"></a>

```java
- (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation
{
    if([Growing handleUrl:url]){
        return YES;
    }
    ...
    return NO;
}
```

&#x20;若您在 AppDelegate 中实现了以下一个或多个方法，请在已实现的函数中，调用`[Growing handleUrl:]`

```java
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
```

&#x20;若以上所有方法均未实现，请实现以下方法并调用`[Growing handleUrl:]`

```
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation
```

&#x20;实际情况可能很复杂，请在调试时确保函数`[Growing handleUrl:]`会被执行到`。`

### &#x20;**3.添加初始化函数**

&#x20;在AppDelegate 中引入`#import "Growing.h"`并添加启动方法

```java
#import "Growing.h"
- (BOOL)application:(UIApplication*)application
    didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
      ...
      //启动GrowingIO
     [Growing startWithAccountId:@"xxxxxxxxxxxxxxxx"]; //替换为您的ID
     // 其他配置
     // 开启Growing调试日志可以开启日志
     // [Growing setEnableLog:YES];
  }
```

&#x20;**请确保将代码添加在上面描述的位置，添加到其他函数中或者异步block 中可能导致数据不准确！**

### **4.App Store 提交应用注意事项** <a href="#id-4app-store-ti-jiao-ying-yong-zhu-yi-shi-xiang" id="id-4app-store-ti-jiao-ying-yong-zhu-yi-shi-xiang"></a>

如果您添加了库**AdSupport.framework**, GrowingIO则会启用 IDFA，所以在向 App Store 提交应用时，需要：

* 对于问题 **Does this app use the Advertising Identifier (IDFA)**，选择 **YES**。
* 对于选项**Attribute this app installation to a previously served advertisement**，打勾。
* 对于选项**Attribute an action taken within this app to a previously served advertisement**，打勾。

> **为什么 GrowingIO 使用 IDFA?** GrowingIO 使用 IDFA 来做来源管理激活设备的精确匹配，让你更好的衡量广告效果。如果您不希望启用IDFA，可以选择不引入 AdSupport.framework

至此，您的SDK安装就成功了。

## 自定义事件和变量 API  <a href="#zi-ding-yi-shi-jian-he-bian-liang-api" id="zi-ding-yi-shi-jian-he-bian-liang-api"></a>

​

&#x20;您的APP或网页在集成了 GrowingIO 的 SDK 之后，它将会自动地为您采集一系列用户行为数据，并在 GrowingIO 分析后台供您制成数据分析报表。除上述的用户行为数据（或称为无埋点数据）之外，GrowingIO 还提供了多种 API 接口，供您上传一些[自定义事件](https://docs.growingio.com/docs/data-definition/custom-event/)和[变量](https://docs.growingio.com/docs/data-definition/custom-event/event-variable)，下面介绍自定义事件和变量 API 使用方法。

SDK 提供多种不同类型的API，请根据您的实际需要正确地调用。

```java
// 发送自定义事件 API
+ (void)track:(NSString *)eventId;
+ (void)track:(NSString *)eventId withNumber:(NSNumber *)number;
+ (void)track:(NSString *)eventId withNumber:(NSNumber *)number andVariable:(NSDictionary<NSString *, NSObject *> *)variable;
+ (void)track:(NSString *)eventId withVariable:(NSDictionary<NSString *, NSObject *> *)variable;
​
// 发送转化变量 API
+ (void)setEvarWithKey:(NSString *)key andStringValue:(NSString *)stringValue;
+ (void)setEvarWithKey:(NSString *)key andNumberValue:(NSNumber *)numberValue;
+ (void)setEvar:(NSDictionary<NSString *, NSObject *> *)variable;
​
// 发送用户变量 API
+ (void)setPeopleVariableWithKey:(NSString *)key andStringValue:(NSString *)stringValue;
+ (void)setPeopleVariableWithKey:(NSString *)key andNumberValue:(NSNumber *)numberValue;
+ (void)setPeopleVariable:(NSDictionary<NSString *, NSObject *> *)variable;
​
// 访问用户变量 API
+ (void)setVisitor:(NSDictionary<NSString *, NSObject *> *)variable;
​
// 设置登录用户ID API
+ (void)setUserId:(NSString *)userId;
​
// 清除登录用户ID API
+ (void)clearUserId;
```

### track <a href="#track" id="track"></a>

发送一个自定义事件。在添加所需要发送的事件代码之前，需要在打点管理用户界面声明事件以及事件级变量。

#### 参数说明： <a href="#can-shu-shuo-ming" id="can-shu-shuo-ming"></a>

| 参数名称               | 参数类型        | 是否必须 | 说明                                                   |
| ------------------ | ----------- | ---- | ---------------------------------------------------- |
| eventId            | String      | 是    | 事件标识符                                                |
| number             | Number      | 否    | 事件的数值，没有number参数时，事件默认加1；当出现number参数时，事件自增number的数值。 |
| eventLevelVariable | JSON Object | 否    | 事件发生时所伴随的维度信息。                                       |

**参数限制条件：**

| 参数名称               | 限制条件                                                                                                                                                                    |
| ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| eventId            | 英文数字组合的字符串，不能为 nil 或者""，长度小于等于50，且不能含有特殊字符                                                                                                                              |
| number             | 正整数或浮点数                                                                                                                                                                 |
| eventLevelVariable | <p>不能为nil；eventLevelVariable 内部不允许含有<code>JSONObject</code>或者<code>JSONArray；</code></p><p><code>key</code> 长度限制小于等于50，<code>value</code> 长度限制小等于1000，值不能为空串，也就是""。</p> |

```java
// track API原型
+ (void)track:(NSString *)eventId;
+ (void)track:(NSString *)eventId withNumber:(NSNumber *)number;
+ (void)track:(NSString *)eventId withNumber:(NSNumber *)number andVariable:(NSDictionary<NSString *, NSObject *> *)variable;
+ (void)track:(NSString *)eventId withVariable:(NSDictionary<NSString *, NSObject *> *)variable;
```

```java
// track API调用示例一
[Growing track:@"registerSuccess"];
```

```java
// track API调用示例二
[Growing track:@"registerSuccess" withVariable:@{@"gender":@"male", @"age":@"21"}];
```

```java
// track API调用示例三
[Growing track:@"loanAmount" withNumber:@800000 andVariable:@{@"loanType":@"houseMortgage", @"province":@"Zhejiang"}];
```

​

### setEvar <a href="#setevar" id="setevar"></a>

发送一个转化信息用于高级归因分析，在添加代码之前必须在打点管理界面上声明转化变量。

#### 参数说明： <a href="#can-shu-shuo-ming-1" id="can-shu-shuo-ming-1"></a>

| 参数名称                | 参数类型        | 是否必须 | 说明           |
| ------------------- | ----------- | ---- | ------------ |
| key                 | String      | 否    | 转化变量的标识符     |
| Value               | String      | 否    | 转化变量的值       |
| conversionVariables | JSON Object | 否    | 转化变量用于高级归因分析 |

**参数限制条件：**

| 参数名称                    | 限制条件                                                                                                                                                                          |
| ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| key                     | 不能为 nil 或者""，长度小于等于50                                                                                                                                                         |
| Value                   | 变量不为nil或者""，若为字符串则长度应小于等于 1000                                                                                                                                                |
| conversionLevelVariable | <p>不能为nil; conversionLevelVariable 内部不允许含有<code>JSONObject</code>或者<code>JSONArray；</code></p><p><code>key</code> 长度限制小于等于50，<code>value</code> 长度限制小等于1000，值不能为空串，也就是""。</p> |

```java
// setEvar API原型
+ (void)setEvarWithKey:(NSString *)key andStringValue:(NSString *)stringValue;
+ (void)setEvarWithKey:(NSString *)key andNumberValue:(NSNumber *)numberValue;
+ (void)setEvar:(NSDictionary<NSString *, NSObject *> *)variable;
```

```java
// setEvar API调用示例一
[Growing setEvarWithKey:@"campaignId" andStringValue:@"1234567890"];
```

```java
// setEvar API调用示例二
[Growing setEvar:@{@"campaignId":@"12345", @"campaignOwner":@"Li Si"}];
```

### setPeopleVariable <a href="#setpeoplevariable" id="setpeoplevariable"></a>

发送用户信息用于用户信息相关分析，在添加代码之前必须在打点管理界面上声明用户变量。

#### 参数说明： <a href="#can-shu-shuo-ming-2" id="can-shu-shuo-ming-2"></a>

| 参数名称              | 参数类型        | 是否必须 | 说明              |
| ----------------- | ----------- | ---- | --------------- |
| key               | String      | 否    | 用户变量的标识符        |
| value             | String      | 否    | 用户变量的值          |
| customerVariables | JSON Object | 否    | 用户变量用于用户信息相关的分析 |

**参数限制条件：**

| 参数名称              | 限制条件                                                                                                                                                                   |
| ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| key               | 不能为nil或""，长度小于等于50                                                                                                                                                     |
| value             | 变量不为nil或者""，若为字符串则长度应小于等于 1000                                                                                                                                         |
| customerVariables | <p>不能为nil;customerVarialbes 内部不允许含有<code>JSONObject</code>或者<code>JSONArray；</code></p><p><code>key</code> 长度限制小于等于50，<code>value</code> 长度限制小等于1000，值不能为空串，也就是""。</p> |

```java
// setPeopleVariable API原型
+ (void)setPeopleVariableWithKey:(NSString *)key andStringValue:(NSString *)stringValue;
+ (void)setPeopleVariableWithKey:(NSString *)key andNumberValue:(NSNumber *)numberValue;
+ (void)setPeopleVariable:(NSDictionary<NSString *, NSObject *> *)variable;
```

```java
// setPeopleVariable API调用示例一
[Growing setPeopleVariableWithKey:@"gender" andStringValue:@"male"];
```

```java
// setPeopleVariable API调用示例二
[Growing setPeopleVariable:@{@"gender":@"male", @"age":@"25"}];
```

### setVisitor <a href="#setvisitor" id="setvisitor"></a>

#### 2.4.0以上版本支持 <a href="#id-2-4-0-yi-shang-ban-ben-zhi-chi" id="id-2-4-0-yi-shang-ban-ben-zhi-chi"></a>

当用户未登录时，定义用户属性变量，也可用于A/B测试上传标签。

#### 参数说明： <a href="#can-shu-shuo-ming-3" id="can-shu-shuo-ming-3"></a>

| 参数名称     | 参数类型        | 是否必须 | 说明     |
| -------- | ----------- | ---- | ------ |
| variable | JSON Object | 是    | 访问用户信息 |

**参数限制条件：**

| 参数名称     | 限制条件                                                                                                                                                                       |
| -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| variable | <p>不能为<code>nil;variable</code> 内部不允许含有<code>JSONObject</code>或者<code>JSONArray；</code></p><p><code>key</code> 长度限制小于等于50，<code>value</code> 长度限制小等于1000，值不能为空串，也就是""。</p> |

```java
// setVisitor 访问用户变量 API原型
+ (void)setVisitor:(NSDictionary<NSString *, NSObject *> *)variable;
```

```java
// setVisitor API调用示例
[Growing setVisitor:@{@"gender":@"male", @"age":@"25"}];
```

### setUserId <a href="#setuserid" id="setuserid"></a>

当用户登录之后调用setUserId API，设置登录用户ID。

#### 参数说明： <a href="#can-shu-shuo-ming-4" id="can-shu-shuo-ming-4"></a>

| 参数名称   | 参数类型   | 是否必须 | 说明        |
| ------ | ------ | ---- | --------- |
| userId | String | 是    | 用户的登录用户ID |

**参数限制条件：**

| 参数名称   | 限制条件                                                                           |
| ------ | ------------------------------------------------------------------------------ |
| userId | 英文数字组合的字符串，长度小于等于1000，且不能含有特殊字符，不允许传空、`""` 或者`nil`，如有清除操作，请调用 `clearUserId` 方法 |

```java
// setUserId API原型
+ (void)setUserId:(NSString *)userId;
```

```java
// setuserId API调用示例
[Growing setUserId:@"1234567890"];
```

{% hint style="warning" %}
如果您的应用是App，且每次用户升级App版本时无需重新登录的话，建议在用户每次升级App版本后初次访问时重新调用上述 setUserId 方法。
{% endhint %}

### clearUserId <a href="#clearuserid" id="clearuserid"></a>

当用户登出之后调用clearUserId，清除已经设置的登录用户ID。

```java
// clearUserId API原型
+ (void)clearUserId;
```

```java
// clearUserId API调用示例
[Growing clearUserId];
```

## 埋点SDK支持的其他接口 <a href="#mai-dian-sdk-zhi-chi-de-qi-ta-jie-kou" id="mai-dian-sdk-zhi-chi-de-qi-ta-jie-kou"></a>

{% hint style="danger" %}
**GrowingIO 埋点 SDK 仅自动采集设备信息和您埋点内容数据，对比无埋点 SDK ，埋点 SDK 少很多 API， 请勿在埋点 SDK 中调用无埋点 SDK 接口。**
{% endhint %}

埋点SDK除了支持上面的打点事件，还支持以接口，接口详情请参考[IOS SDK API](https://docs.growingio.com/docs/sdk-integration/ios-sdk/ios-sdk-api)中的说明：

```java
1，如果需要采样设置一个采样值  0.01即1% 0.001即1‰  最多支持小数点后5位
+ (void)startWithAccountId:(NSString*)accountId withAppId:(NSString*)appId withSampling:(CGFloat)sampling;
+ (void)startWithAccountId:(NSString*)accountId withSampling:(CGFloat)sampling;
​
2，默认采样100%
+ (void)startWithAccountId:(NSString*)accountId withAppId:(NSString*)appId;
+ (void)startWithAccountId:(NSString*)accountId;
​
3，命令行输出调试日志
+ (void)setEnableLog:(BOOL)enableLog;
+ (BOOL)getEnableLog;
​
4，若使用加密功能,请在UI元素初始化之前设置此函数
+ (void)setEncryptStringBlock:(NSString*(^)(NSString*string))block;
​
5，以下函数设置后会覆盖原有设置
// 并且只会在第一次安装后调用以保证同一设备的设备ID相同
// 请在方法startWithAccountId之前调用
// 使用自定义的ID 自定义ID长度不可大于64 否则会被抛弃NSUUID的UUIDString长度为36
+ (void)setDeviceIDModeToCustomBlock:(NSString*(^)(void))customBlock;
​
6，deeplink广告落地页参数回调设置
+ (void)registerDeeplinkHandler:(void(^)(NSDictionary*params, NSError*error))handler;
​
7，Universallink广告落地页参数回调设置
+ (void)registerUniversallinkHandler:(void(^)(NSDictionary*params, NSError*error))handler;
​
8，该函数请在main函数第一行调用APP启动后将不允许修改采集模式
+ (void)setAspectMode:(GrowingAspectMode)aspectMode;
+ (GrowingAspectMode)getAspectMode;
​
9，是否允许发送基本性能诊断信息，默认为开
+ (void)setEnableDiagnose:(BOOL)enable;
​
10，全局不发送统计信息
+ (void)disable;
​
11，设置发送数据的时间间隔（单位为秒）
+ (void)setFlushInterval:(NSTimeInterval)interval;
+ (NSTimeInterval)getFlushInterval;
​
12，设置每天使用数据网络（2G、3G、4G）上传的数据量的上限（单位是KB）
+ (void)setDailyDataLimit:(NSUInteger)numberOfKiloByte;
+ (NSUInteger)getDailyDataLimit;
​
13，设置数据收集平台服务器地址
+ (void)setTrackerHost:(NSString*)host;
​
14，设置设备报活服务器地址
+ (void)setReportHost:(NSString*)host;
​
15，设置数据查看平台服务器地址
+ (void)setDataHost:(NSString*)host;
​
16，设置数据后台服务器地址
+ (void)setGtaHost:(NSString*)host;
​
17，设置数据后台服务器地址
+ (void)setWsHost:(NSString*)host;
​
18，设置zone信息
+ (void)setZone:(NSString*)zone;
​
19，设置GDPR 生效
+ (void)disableDataCollect;
​
20，设置GDPR 失效
+ (void)enableDataCollect;
​
21，获取当前设备id
+ (NSString*)getDeviceId;
​
22，获取当前uid
+ (NSString*)getVisitUserId;
​
23，获取当前访问id
+ (NSString*)getSessionId;
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://growingio.gitbook.io/old_docs/sdk-integration/ios-sdk/ios-mai-dian-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
