美文网首页
GrowingAnalytics源码阅读-初始化

GrowingAnalytics源码阅读-初始化

作者: iOS_Yee | 来源:发表于2021-09-21 16:37 被阅读0次

    GrowingAnalytics

    Github地址 : https://github.com/growingio/growingio-sdk-ios-autotracker

    源码

    GrowingAppLifecycleDelegate源码实现

    相关类

    • GrowingDynamicProxy
    • GrowingAutotracker
    • GrowingRealTracker
    • GrowingRealAutotracker
    • GrowingTrackConfiguration
    • GrowingAutotrackConfiguration

    从代码开始

       //1.声明一个GrowingTrackConfiguration示例对象。
      // 2. GrowingAutotracker执行startWithConfiguration方法。
        GrowingTrackConfiguration *configuration = [GrowingTrackConfiguration configurationWithProjectId:@"aaaa"];
        [GrowingAutotracker startWithConfiguration:configuration launchOptions:nil];
    

    GrowingTrackConfiguration

    // 项目projectId
    @property(nonatomic, copy, readonly) NSString *projectId;
    // 控制日志打印,日志上传
    @property(nonatomic, assign) BOOL debugEnabled;
    //控制上传大小,不知道是单次还是累加?
    @property(nonatomic, assign) NSUInteger cellularDataLimit;
    //控制数据上传间隔 ,默认是15
    @property(nonatomic, assign) NSTimeInterval dataUploadInterval;
    // sessionId 刷新时间间隔,默认为30秒,详解的代码可见:refreshSessionId方法
    @property(nonatomic, assign) NSTimeInterval sessionInterval;
    // 没看出来什么作用
    @property(nonatomic, assign) BOOL dataCollectionEnabled;
    //源码上没这个功能
    @property(nonatomic, assign) BOOL uploadExceptionEnable;
    //数据采集上传默认服务器
    @property(nonatomic, copy) NSString *dataCollectionServerHost;
    // 上传日志时,过滤SDK内部事件不上传到后台,默认为0
    @property(nonatomic, assign) NSUInteger excludeEvent;
    //  过滤内部事件 默认为0
    @property(nonatomic, assign) NSUInteger ignoreField;
    

    GrowingAutotracker

    static GrowingAutotracker *sharedInstance = nil;
    + (void)startWithConfiguration:(GrowingTrackConfiguration *)configuration launchOptions:(NSDictionary *)launchOptions {
      //1. 主线程判断
     // 2. 项目id空判断
     // 3. sharedInstance初始化,利用NSProxy与消息转化将GrowingRealAutotracker的具体实现转移到 GrowingAutotracker修饰的实体对象上。
     // 4.用户相关操作
        if (![NSThread isMainThread]) {
            @throw [NSException exceptionWithName:@"初始化异常" reason:@"请在applicationDidFinishLaunching中调用startWithConfiguration函数,并且确保在主线程中" userInfo:nil];
        }
        if (!configuration.projectId.length) {
            @throw [NSException exceptionWithName:@"初始化异常" reason:@"ProjectId不能为空" userInfo:nil];
        }
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            GrowingRealAutotracker *autotracker = [GrowingRealAutotracker trackerWithConfiguration:configuration launchOptions:launchOptions];
            sharedInstance = [[self alloc] initWithRealAutotracker:autotracker];
            [[GrowingSession currentSession] generateVisit];
        });
    }
    

    GrowingRealAutotracker与GrowingRealTracker

    GrowingRealAutotracker继承自GrowingRealTracker类,执行初始化方法,会先后执行GrowingRealTracker与GrowingRealAutotracker内部对应的initWithConfiguration方法。

    - (instancetype)initWithConfiguration:(GrowingTrackConfiguration *)configuration launchOptions:(NSDictionary *)launchOptions {
        self = [super init];
        if (self) {
            _configuration = [configuration copyWithZone:nil];
            _launchOptions = [launchOptions copy];
            GrowingConfigurationManager.sharedInstance.trackConfiguration = self.configuration;
            
             //DDLog日志设置
            [self loggerSetting];
            // 应用生命周期,将需要获取"程序启动","前后台切换,"等事件的实例通过内部维护一个list,当事件触发时,将事件分发给list存储的对象中。这样看来是想让list种的组件完全独立,生命周期由一个中心组件分发。【注意点:内部有NSLock锁】
            [GrowingAppLifecycle.sharedInstance setupAppStateNotification];
            //用户账号信息设置,sessionId刷新操作。内部同样维护一个list,当用户userid发送改变时,将会收到账号变更消息。
            [GrowingSession startSession];
            //外部链接打开应用,重写[UIApplication sharedApplication].delegate的openURL等方法。
            [GrowingAppDelegateAutotracker track];
           // BeeHive注册模块实现。https://github.com/alibaba/BeeHive
            [[GrowingModuleManager sharedInstance] registedAllModules];
            [[GrowingModuleManager sharedInstance] triggerEvent:GrowingMInitEvent];
            // 各个Module初始化init之后再进行事件定时发送
           // 埋点数据统计上传初始化,FMDB,定时器,channel相关操作。
            [[GrowingEventManager sharedInstance] configChannels];
            [[GrowingEventManager sharedInstance] startTimerSend];
      
           // 打印Growing当前版本号
            [self versionPrint];
         
           // 打印过滤信息
            [self filterLogPrint];
    
           // 常见UI组件Hook方法
            [self addAutoTrackSwizzles];
           // UIViewController生命周期管理操作,内部有一个容器,负责DidAppear,DidDisappear操作
            [GrowingPageManager.sharedInstance start];
           //  app生命周期的接收,UIViewController生命周期管理,内部注册了一个Runloop,负责上传当前页面的UiView相关的node路径。
            [GrowingImpressionTrack.sharedInstance start];
        }
    
        return self;
    }
    
    

    相关文章

      网友评论

          本文标题:GrowingAnalytics源码阅读-初始化

          本文链接:https://www.haomeiwen.com/subject/cbvwgltx.html