iOS中自定义界面统计(统计一定时间内访问次数,以及停留时间)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GuoFengIOS/article/details/51887362

由于我们的app做的是p2p以及奇葩的产品经理,好好的友盟统计不用,非要用自己和服务器交互的统计,只能自己写,然后每隔一段时间上传一次咯!


直接上代码吧!

1.首先自定义一个类:(用于统计次数和停留时间的方法)

//

//  statistics.h

//  WuXianMoney

//

//  Created by GF on 16/3/29.

//  Copyright © 2016 WXDL. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface statistics : NSObject

/*

 简易统计界面停留时间/以及次数:

 */



+(void)staticsvisitTimesDataWithViewControllerType:(NSString *)type;//统计次数

+(void)staticsstayTimeDataWithType:(NSString *)type WithController:(NSString *)name;//统计时间计算,type: 1代表进入界面 2 :出界面

+(NSString *)staticsTimeDataWithController:(NSString *)name;//统计时间,一直停留的

+(NSString *)getStayTime:(NSString *)controller;

+(void)removeLocalDataWithController:(NSString *)name;//清除本地数据;

+(NSDictionary *)packageDictionary:(NSString *)name WithType:(NSString *)type;//打包字典

@end


//

//  statistics.m

//  WuXianMoney

//

//  Created by GF on 16/3/29.

//  Copyright © 2016 WXDL. All rights reserved.

//


#import "statistics.h"


@implementation statistics

/*

 

 key-Value值对应关系:

   界面名称:----访问次数

   1界面名称----进入界面时刻

   2界面名称-----离开界面时刻

   界面名称histime------界面停留历史时间

   界面名称time--------界面停留总时间

   界面名称oppositeTime----相对时间

 */


#pragma mark-

#pragma mark 统计次数

+(void)staticsvisitTimesDataWithViewControllerType:(NSString *)type

{


    NSString * timesStart=[ZJDataSaver getStringForKey:type]?[ZJDataSaver getStringForKey:type]:nil;

    int add=[timesStart intValue];

    add++;

    [ZJDataSaver saveString:[NSString stringWithFormat:@"%d",add] forKey:type];

    

}

+(void)staticsstayTimeDataWithType:(NSString *)type WithController:(NSString *)name//计算一次在该界面停留的时间

{

    switch ([type intValue]) {

        case 1://用来获取进入界面的时刻

        {

        

            NSDate * date=[NSDate date];

            NSString * dateStr=[NSString stringWithFormat:@"%0.f",[date timeIntervalSince1970]];

            [ZJDataSaver saveString:dateStr forKey:[NSString stringWithFormat:@"%@%@",type,name]];

        }

            

            break;

            

        case 2://用来获取离开界面的时刻    /**/

            

            

    

        {

            NSDate * date=[NSDate date];

            NSString * dateStr=[NSString stringWithFormat:@"%0.f",[date timeIntervalSince1970]];

            [ZJDataSaver saveString:dateStr forKey:[NSString stringWithFormat:@"%@%@",type,name]];

            NSString * startTime=[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@%@",@"1",name]]?[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@%@",@"1",name]]:@"0";//进入界面时间

            NSString * endTime=[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@%@",@"2",name]]?[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@%@",@"2",name]]:@"0";;//离开界面时间

            long  time=0;

            if([endTime longLongValue]==0)

            {

                time=0;

                [ZJDataSaver removeStringForKey:[NSString stringWithFormat:@"%@%@",@"1",name]];//移除开始时间

                [ZJDataSaver removeStringForKey:[NSString stringWithFormat:@"%@%@",@"2",name]];

                

            }

            else

            {

                time=[endTime longLongValue]-[startTime longLongValue];

                [ZJDataSaver removeStringForKey:[NSString stringWithFormat:@"%@%@",@"1",name]];

                [ZJDataSaver removeStringForKey:[NSString stringWithFormat:@"%@%@",@"2",name]];

            }

            

            

            NSString * hisTime=[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@histime",name]];//历史时间

            long ZTime=[hisTime longLongValue]+time;

            NSString * Time=[NSString stringWithFormat:@"%ld",ZTime];//该界面的总时间

            [ZJDataSaver saveString:Time forKey:[NSString stringWithFormat:@"%@time",name]];//存储总时间

            [ZJDataSaver saveString:Time forKey:[NSString stringWithFormat:@"%@histime",name]];//存储历史时间

 

        }

            break;

        default:

            break;

    }

    

}

+(NSString *)staticsTimeDataWithController:(NSString *)name

{

    

    NSString * hisTime=[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@histime",name]];//历史时间

    NSString * startTime=[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@%@",@"1",name]]?[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@%@",@"1",name]]:@"0";//进入界面时间

    long oppositeTime=[hisTime longLongValue]-[startTime longLongValue];//相对时间,针对只有开始时间,一直停留在该界面的处理情况

    NSDate * date=[NSDate date];

    NSString * dateStr=[NSString stringWithFormat:@"%0.f",[date timeIntervalSince1970]];

    long time=[dateStr longLongValue]+oppositeTime;

    //[ZJDataSaver saveString:[NSString stringWithFormat:@"%ld",time] forKey:[NSString stringWithFormat:@"%@ztime",name]];

    [ZJDataSaver saveString:dateStr forKey:[NSString stringWithFormat:@"%@%@",@"1",name]];

    [statistics staticsvisitTimesDataWithViewControllerType:name];

    return [NSString stringWithFormat:@"%ld",time];


}

+(NSString *)getStayTime:(NSString *)controller

{

    NSString * time=[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"1%@",controller]]&&![ZJDataSaver getStringForKey:[NSString stringWithFormat:@"2%@",controller]]?[statistics staticsTimeDataWithController:[NSString stringWithFormat:@"%@",controller]]:[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@time",controller]];

    return time;

    

}

+(void)removeLocalDataWithController:(NSString *)name

{

    [ZJDataSaver removeStringForKey:[NSString stringWithFormat:@"%@",name]];

    [ZJDataSaver removeStringForKey:[NSString stringWithFormat:@"%@time",name]];

    [ZJDataSaver removeStringForKey:[NSString stringWithFormat:@"%@histime",name]];

   Save(@"",@"StaticsArray");

   Remove(@"StaticsArray");

    

    

}

+(NSDictionary *)packageDictionary:(NSString *)name WithType:(NSString *)type//打包字典

{

    NSString * time=[statistics getStayTime:[NSString stringWithFormat:@"%@",name]];//判断是否一直停留在该界面,离开取总时间,停留取相对时间

    

    NSMutableArray * dataArray=Get(@"StaticsArray")?Get(@"StaticsArray"):[NSMutableArray array];

    NSDictionary * dic=@{@"visitName":[NSString stringWithFormat:@"%@",name],@"visitType":[NSString stringWithFormat:@"%@",type],@"visitTimes":[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@",name]]?[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@",name]]:@"0",@"staySeconds":time?time:@"0"};

    [dataArray addObject:dic];

    Save(dataArray, @"StaticsArray");

    NSDictionary * BigDic=@{@"list":dataArray};

    return BigDic;

}





@end




//

//  statistics.m

//  WuXianMoney

//

//  Created by GF on 16/3/29.

//  Copyright © 2016 WXDL. All rights reserved.

//


#import "statistics.h"


@implementation statistics

/*

 

 key-Value值对应关系:

   界面名称:----访问次数

   1界面名称----进入界面时刻

   2界面名称-----离开界面时刻

   界面名称histime------界面停留历史时间

   界面名称time--------界面停留总时间

   界面名称oppositeTime----相对时间

 */


#pragma mark-

#pragma mark 统计次数

+(void)staticsvisitTimesDataWithViewControllerType:(NSString *)type

{


    NSString * timesStart=[ZJDataSaver getStringForKey:type]?[ZJDataSaver getStringForKey:type]:nil;

    int add=[timesStart intValue];

    add++;

    [ZJDataSaver saveString:[NSString stringWithFormat:@"%d",add] forKey:type];

    

}

+(void)staticsstayTimeDataWithType:(NSString *)type WithController:(NSString *)name//计算一次在该界面停留的时间

{

    switch ([type intValue]) {

        case 1://用来获取进入界面的时刻

        {

        

            NSDate * date=[NSDate date];

            NSString * dateStr=[NSString stringWithFormat:@"%0.f",[date timeIntervalSince1970]];

            [ZJDataSaver saveString:dateStr forKey:[NSString stringWithFormat:@"%@%@",type,name]];

        }

            

            break;

            

        case 2://用来获取离开界面的时刻    /**/

            

            

    

        {

            NSDate * date=[NSDate date];

            NSString * dateStr=[NSString stringWithFormat:@"%0.f",[date timeIntervalSince1970]];

            [ZJDataSaver saveString:dateStr forKey:[NSString stringWithFormat:@"%@%@",type,name]];

            NSString * startTime=[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@%@",@"1",name]]?[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@%@",@"1",name]]:@"0";//进入界面时间

            NSString * endTime=[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@%@",@"2",name]]?[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@%@",@"2",name]]:@"0";;//离开界面时间

            long  time=0;

            if([endTime longLongValue]==0)

            {

                time=0;

                [ZJDataSaver removeStringForKey:[NSString stringWithFormat:@"%@%@",@"1",name]];//移除开始时间

                [ZJDataSaver removeStringForKey:[NSString stringWithFormat:@"%@%@",@"2",name]];

                

            }

            else

            {

                time=[endTime longLongValue]-[startTime longLongValue];

                [ZJDataSaver removeStringForKey:[NSString stringWithFormat:@"%@%@",@"1",name]];

                [ZJDataSaver removeStringForKey:[NSString stringWithFormat:@"%@%@",@"2",name]];

            }

            

            

            NSString * hisTime=[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@histime",name]];//历史时间

            long ZTime=[hisTime longLongValue]+time;

            NSString * Time=[NSString stringWithFormat:@"%ld",ZTime];//该界面的总时间

            [ZJDataSaver saveString:Time forKey:[NSString stringWithFormat:@"%@time",name]];//存储总时间

            [ZJDataSaver saveString:Time forKey:[NSString stringWithFormat:@"%@histime",name]];//存储历史时间

 

        }

            break;

        default:

            break;

    }

    

}

+(NSString *)staticsTimeDataWithController:(NSString *)name

{

    

    NSString * hisTime=[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@histime",name]];//历史时间

    NSString * startTime=[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@%@",@"1",name]]?[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@%@",@"1",name]]:@"0";//进入界面时间

    long oppositeTime=[hisTime longLongValue]-[startTime longLongValue];//相对时间,针对只有开始时间,一直停留在该界面的处理情况

    NSDate * date=[NSDate date];

    NSString * dateStr=[NSString stringWithFormat:@"%0.f",[date timeIntervalSince1970]];

    long time=[dateStr longLongValue]+oppositeTime;

    //[ZJDataSaver saveString:[NSString stringWithFormat:@"%ld",time] forKey:[NSString stringWithFormat:@"%@ztime",name]];

    [ZJDataSaver saveString:dateStr forKey:[NSString stringWithFormat:@"%@%@",@"1",name]];

    [statistics staticsvisitTimesDataWithViewControllerType:name];

    return [NSString stringWithFormat:@"%ld",time];


}

+(NSString *)getStayTime:(NSString *)controller

{

    NSString * time=[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"1%@",controller]]&&![ZJDataSaver getStringForKey:[NSString stringWithFormat:@"2%@",controller]]?[statistics staticsTimeDataWithController:[NSString stringWithFormat:@"%@",controller]]:[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@time",controller]];

    return time;

    

}

+(void)removeLocalDataWithController:(NSString *)name

{

    [ZJDataSaver removeStringForKey:[NSString stringWithFormat:@"%@",name]];

    [ZJDataSaver removeStringForKey:[NSString stringWithFormat:@"%@time",name]];

    [ZJDataSaver removeStringForKey:[NSString stringWithFormat:@"%@histime",name]];

   Save(@"",@"StaticsArray");

   Remove(@"StaticsArray");

    

    

}

+(NSDictionary *)packageDictionary:(NSString *)name WithType:(NSString *)type//打包字典

{

    NSString * time=[statistics getStayTime:[NSString stringWithFormat:@"%@",name]];//判断是否一直停留在该界面,离开取总时间,停留取相对时间

    

    NSMutableArray * dataArray=Get(@"StaticsArray")?Get(@"StaticsArray"):[NSMutableArray array];

    NSDictionary * dic=@{@"visitName":[NSString stringWithFormat:@"%@",name],@"visitType":[NSString stringWithFormat:@"%@",type],@"visitTimes":[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@",name]]?[ZJDataSaver getStringForKey:[NSString stringWithFormat:@"%@",name]]:@"0",@"staySeconds":time?time:@"0"};

    [dataArray addObject:dic];

    Save(dataArray, @"StaticsArray");

    NSDictionary * BigDic=@{@"list":dataArray};

    return BigDic;

}



@end



然后在每个视图控制器中调用:

-(void)viewWillAppear:(BOOL)animated

{

    

    [statistics staticsstayTimeDataWithType:@"1" WithController:@"PasswordManager"];

    

    

}


-(void)viewWillDisappear:(BOOL)animated

{

    [statistics  staticsvisitTimesDataWithViewControllerType:@"PasswordManager"];

    [statistics staticsstayTimeDataWithType:@"2" WithController:@"PasswordManager"];

}



最后在入口类统一每隔10min上传一次:


 _controllerArray=[[NSArray alloc]initWithObjects:@"GuideView",@"HotRecommended",@"MarketView",@"FinancialDetailView",@"ProjectDetailView",@"ProvidePlatformView",@"ProtocalStorageView",@"CheckProtocalView",@"WebControllerProtocal",@"BanKListView",@"ExchangeInfo",@"FinancialHtml5View",@"MineView",@"NotificationMessageView",@"MessageTypeView",@"AssetMoreView",@"AccountInfoView",@"NikeNameView",@"PhoneViewPhone",@"VerifyPhoneViewPhone",@"SetPhoneViewPhone",@"PhoneViewEmail",@"VerifyPhoneViewEmail",@"SetPhoneViewEmail",@"AddressView",@"VIPcontroller",@"PasswordManager",@"ModifyLoginPassword",@"SetTradeController",@"ConfirmTradingPassword",@"AssetAboutUs",@"CompanyIntroView",@"PartnerView",@"CreditWebViewController",@"TodayEarningsView",@"TodayEarningsTetailView",@"TradingRecordView",@"TradeDetailView",@"WebControllerBanner",@"RegsiterOrLoginSetPhoneView",@"NewRegisterViewController",@"FindLoginPassword",@"SetPassword",@"VipDetail",nil];//视图标示名称


-(void)startTimer//定时器

{

    __block int timeout = 0;

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);

    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行

    dispatch_source_set_event_handler(_timer, ^{

        if (timeout%600==0&&timeout!=0)

        {

            dispatch_async(dispatch_get_main_queue(), ^{

               

               [self loadRequest];//上传请求

    

            });

        }

        else

        {

//            NSLog(@"%D",timeout);

        }

        timeout++;


    });

    dispatch_resume(_timer);

}


-(void)loadRequest

{

    

    NSDictionary * dic=[[NSDictionary alloc]init];

    

    for(int i=0;i<_controllerArray.count;i++)

    {

    dic=[statistics packageDictionary:_controllerArray[i] WithType:[NSString stringWithFormat:@"%d",i+1]];

    }

    [RecommendRequest upTimeWithDictionary:dic WithResultBlock:^(NSDictionary *resultDic) {

        

        for(int i=0;i<_controllerArray.count;i++)

        {

            [statistics removeLocalDataWithController:_controllerArray[i]];

            

        }

    if ([resultDic[@"resultCode"] intValue]==10000) {

        NSLog(@"ok");

           }

}];




}



最后别忘了每次重新进入的时候把上次数据清空,重新计算







展开阅读全文

没有更多推荐了,返回首页