Skip to content

Commit

Permalink
Merge pull request #3127 from srahim/timob-10773
Browse files Browse the repository at this point in the history
[TIMOB-10773]iOS: Only send ti.geo analytics event once when user sets a ti.geolocation callback
  • Loading branch information
Max Stepanov committed Oct 9, 2012
2 parents b485e06 + 4147d7d commit 00f0149
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions iphone/Classes/GeolocationModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -804,33 +804,52 @@ -(void)setPurpose:(NSString *)reason

}

#pragma mark Geolacation Analytics

-(void)fireApplicationAnalyticsIfNeeded:(NSArray *)locations{
static BOOL analyticsSend = NO;
if (TI_APPLICATION_ANALYTICS && !analyticsSend)
{
analyticsSend = YES;
NSDictionary *todict = [self locationDictionary:[locations lastObject]];
NSDictionary *fromdict = [self locationDictionary:[locations objectAtIndex:0]];//This location could be same as todict value.

NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:todict,@"to",fromdict,@"from",nil];
NSDictionary *geo = [NSDictionary dictionaryWithObjectsAndKeys:data,@"data",@"ti.geo",@"name",@"ti.geo",@"type",nil];

WARN_IF_BACKGROUND_THREAD; //NSNotificationCenter is not threadsafe!
[[NSNotificationCenter defaultCenter] postNotificationName:kTiAnalyticsNotification object:nil userInfo:geo];
}
}

#pragma mark Delegates

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSDictionary *todict = [self locationDictionary:newLocation];

NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:
//Using new delegate instead of the old deprecated method - (void)locationManager:didUpdateToLocation:fromLocation:

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
NSDictionary *todict = [self locationDictionary:[locations lastObject]];

NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:
todict,@"coords",
NUMBOOL(YES),@"success",
nil];

if (TI_APPLICATION_ANALYTICS)
{
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:todict,@"to",[self locationDictionary:oldLocation],@"from",nil];
NSDictionary *geo = [NSDictionary dictionaryWithObjectsAndKeys:data,@"data",@"ti.geo",@"name",@"ti.geo",@"type",nil];
WARN_IF_BACKGROUND_THREAD; //NSNotificationCenter is not threadsafe!
[[NSNotificationCenter defaultCenter] postNotificationName:kTiAnalyticsNotification object:nil userInfo:geo];
}

if ([self _hasListeners:@"location"])
if ([self _hasListeners:@"location"])
{
[self fireEvent:@"location" withObject:event];
}

[self fireApplicationAnalyticsIfNeeded:locations];
[self fireSingleShotLocationIfNeeded:event stopIfNeeded:YES];
}



- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[self locationManager:manager didUpdateLocations:[NSArray arrayWithObjects:oldLocation,newLocation,nil]];
}


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:[error localizedDescription],@"error",
Expand Down

0 comments on commit 00f0149

Please sign in to comment.