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

The container for Google Tag Manager is setting up late in the ios app

Google 跟踪代码管理器的容器在此函数中实例化,因为 iOS 控制主线程,因此此容器设置较晚,而 googleTagContainer returns 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 容器可用后,是否有任何方法可以调用此函数,或者在设置容器的过程完成后,是否有任何方法可以保存数据并发送跟踪数据?

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

假设你有一个实用程序 class 来加载你的容器,并且有一个名为 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];
  });
}