简体   繁体   English

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

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

The container for Google Tag Manager is instantiated in this function, as iOS controls the main thread so this container is setup late and googleTagContainer returns nil once the second function is called : 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];

    }); 
}

Once the following function is called the container isn't set up yet so it escapes the if statement and looses some tracking data. 调用以下函数后,容器尚未设置,因此它转义了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

}

Is there any way to call this function after the Google container becomes available or any way to save data and send the tracking data after the process of setting up the container gets finished? Google容器可用后,是否有任何方法可以调用此函数,或者在设置容器的过程完成后,有什么方法可以保存数据并发送跟踪数据?

Google Tag Manager's 3.15 SDK doesn't support this directly, but you can certainly put together a façade that allows you to push tracking data to an intermediate queue, and then flush that queue when containerAvailable is called. Google跟踪代码管理器的3.15 SDK不直接支持此功能,但是您可以放一个外观,使您可以将跟踪数据推送到中间队列,然后在调用containerAvailable时刷新该队列。

Assuming you have a utility class which loads your container, and has an NSMutableDictionary called queuedEvents , and a dispatch queue _emitQueue , you might have a method like: 假设您有一个实用程序类来加载您的容器,并且有一个名为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];
  }
}

When the container loads, you would flush queuedEvents like so: 当容器加载时,您将像这样冲洗queueedEvents:

-(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];
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM