Google跟踪代码管理器的容器在ios应用中设置较晚

时间:2016-07-05 15:29:45

标签: ios google-analytics google-tag-manager

Google跟踪代码管理器的容器在此函数中实例化,因为iOS控制主线程,因此此容器设置较晚,并且一旦调用第二个函数,googleTagContainer将返回nil:

-(void)containerAvailable:(TAGContainer *)container {

    dispatch_async(dispatch_get_main_queue(), ^{

        //Register custom function call tag or variable handler here if necessary

        [self setGoogleTagContainer:container];

    }); 
}

调用以下函数后,容器尚未设置,因此它会转义if语句并丢失一些跟踪数据。

-(void)trackScreen:(NSString *)screen section:(NSString *)section location:(TWNLocation *)location additionalData:(NSDictionary *)data {

    [super trackScreen:screen section:section location:location additionalData:data];

    //2016.1.8 Google Analytics

    if ([self googleTagContainer] != nil) {

        NSDictionary *googleTrackData = [self googleScreenDataForOmnitureScreen:screen section:section location:location data:data];

        if (googleTrackData != nil) {

            [self submitGoogleTrackingData:googleTrackData];

        } else {

            DMLogDebug(DebugLogTypeTracking, @"Google Analytics tracking data not available for screen: %@, section: %@", screen, section);
        }

    } //end if valid tag

}

在Google容器可用之后有没有办法调用此功能,或者在设置容器的过程完成后以何种方式保存数据并发送跟踪数据?

1 个答案:

答案 0 :(得分:1)

Google跟踪代码管理器的3.15 SDK不直接支持此功能,但您当然可以组合一个外观,允许您将跟踪数据推送到中间队列,然后在调用containerAvailable时刷新该队列。

假设您有一个加载容器的实用程序类,并且有一个名为queuedEvents的NSMutableDictionary和一个调度队列_emitQueue,您可能有一个类似的方法:

- (void)pushDataLayerUpdate:(NSDictionary*)update {
  if (self.container) {
    dispatch_async(_emitQueue, ^{
      [_tagManager.dataLayer push:update];
    });
  } else {
    // The container is not loaded, queue the block.
    [_queuedEvents addObject:update];
  }
}

当容器加载时,你会像这样刷新queuedEvents:

-(void)containerAvailable:(TAGContainer *)container {
  // dispatch any pending events in the background.
  dispatch_async(_emitQueue, ^{
    self.container = container;
    TAGDataLayer *dataLayer = _tagManager.dataLayer;
    for (NSDictionary *update in _queuedEvents) {
      [dataLayer push:update];
    }
    [_queuedEvents removeAllObjects];
  });
}