Search code examples
iosflurry

Recording time spend in a tab using Flurry Analytics


I currently have multiple tabs within my application. What I would like to do is record the amount of time the user spends in the selected tab using Flurry Analytics. Usually I would start recording when viewDidAppear is call and stop recording when the viewDidDisappear method is called.

viewDidAppear

[Flurry logEvent:@"Tab_News" withParameters:nil timed:YES];

viewDidDisappear

[Flurry endTimedEvent:@"Tab_News" withParameters:nil];

My problem is that when a video gets played within the tab the viewDidDisappear and viewDidAppear methods get called even thought the user doesnt physically leave the tab.

Please help me with suggestions as to how I can circumvent this.


Solution

  • Create a BOOL called videoPlaying to track if a video is being played. When you play the video set it to YES. In viewDidDisappear only endTimedEvent if (videoPlaying == NO)

    The same applies to viewDidAppear. If videoPlaying == YES don't logEvent and then set videoPlaying = NO.

    Something like this:

    - (void)viewDidLoad
    {
        _videoPlaying = NO;
    }
    
    - (void)viewDidAppear
    {
        if (_videoPlaying == YES) {
            _videoPlaying = NO;
        } else {
            [Flurry logEvent:@"Tab_News" withParameters:nil timed:YES];
        }
    }
    
    - (void)viewDidDisappear
    {
       if (_videoPlaying == NO) {
          [Flurry endTimedEvent:@"Tab_News" withParameters:nil];
       }
    }
    
    - (void)playVideo
    {
        _videoPlaying = YES;
        // Play video
    }