Skip to content

Commit

Permalink
Beacons and event tracking (#10)
Browse files Browse the repository at this point in the history
* Version bump to 2.2.0
Add beacon helper for ibeacons

Update stats call home / device token registration / location tracking to use the event tracking bus.

* Last network calls getting pumped into event stream.

* Tweak method name for clarity.

* Update the enum.

* Enums, refactor the poking of event service to allow instant sync.

* Enums for event types.

* expect the CLBeacon object for the send beacon helper, we can then do the munging of beacon ID and forward.

* Fix version number for podspec.
Make sure KumulosEvent is internal.

* Fix the doc block for the public method.
Chop immediate flush from the public method, leave it internal for now.
Dont need to flush push opens immediately, can do this on backgrounding.

* Leave the beacon params as separate keys for future querying

* Bump version in readme.

* Add type for iBeacon call homes.
  • Loading branch information
azlindsay authored and cgwyllie committed Mar 14, 2018
1 parent ab7a3f1 commit fbfc8cd
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 38 deletions.
4 changes: 2 additions & 2 deletions Cartfile.resolved
@@ -1,2 +1,2 @@
github "Alamofire/Alamofire" "4.6.0"
github "kstenerud/KSCrash" "1.15.16"
github "Alamofire/Alamofire" "4.7.0"
github "kstenerud/KSCrash" "1.15.18"
2 changes: 1 addition & 1 deletion Carthage/Checkouts/Alamofire
Submodule Alamofire updated 153 files
34 changes: 34 additions & 0 deletions Kumulos+Engage.swift
@@ -0,0 +1,34 @@
//
// Kumulos+Engage.swift
// KumulosSDK
//
// Created by Andrew Lindsay on 08/03/2018.
// Copyright © 2018 Kumulos. All rights reserved.
//

import CoreLocation

public extension Kumulos{

public static func sendLocationUpdate(location: CLLocation) {
let parameters = [
"lat" : location.coordinate.latitude,
"lng" : location.coordinate.longitude
]

Kumulos.trackEvent(eventType: KumulosEvent.ENGAGE_LOCATION_UPDATED, properties: parameters, immediateFlush: true)
}

public static func sendiBeaconProximity(beacon: CLBeacon) {

let parameters = [
"type": 1,
"uuid": beacon.proximityUUID.uuidString,
"major": beacon.major.stringValue,
"minor": beacon.minor.stringValue,
"proximity" : beacon.proximity.rawValue
] as [String : Any];

Kumulos.trackEvent(eventType: KumulosEvent.ENGAGE_BEACON_ENTERED_PROXIMITY, properties: parameters, immediateFlush: true)
}
}
4 changes: 4 additions & 0 deletions KumulosSDK.xcodeproj/project.pbxproj
Expand Up @@ -22,6 +22,7 @@
1293720B2006378D00EBC8F4 /* AnalyticsHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1293720A2006378D00EBC8F4 /* AnalyticsHelper.swift */; };
12A6BD871FCD7A7500D59D37 /* KSConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12A6BD861FCD7A7400D59D37 /* KSConfig.swift */; };
12B30580200906000036498F /* KSCrash.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 12937235200671EE00EBC8F4 /* KSCrash.framework */; };
6FE6B5762051550E0025CBC0 /* Kumulos+Engage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FE6B5752051550E0025CBC0 /* Kumulos+Engage.swift */; };
A13430A11F013D6E00D185FD /* Kumulos+Crash.swift in Sources */ = {isa = PBXBuildFile; fileRef = A13430A01F013D6E00D185FD /* Kumulos+Crash.swift */; };
A13430B71F014E5D00D185FD /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A13430B61F014E5D00D185FD /* SystemConfiguration.framework */; };
A13430B91F014E6500D185FD /* MessageUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A13430B81F014E6500D185FD /* MessageUI.framework */; };
Expand Down Expand Up @@ -51,6 +52,7 @@
12991B862007C2160000D492 /* KSCrash.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = KSCrash.framework; sourceTree = BUILT_PRODUCTS_DIR; };
12A6BD861FCD7A7400D59D37 /* KSConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = KSConfig.swift; path = Sources/KSConfig.swift; sourceTree = "<group>"; };
12F1E5CF2007C16E005151D5 /* KSCrash.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = KSCrash.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6FE6B5752051550E0025CBC0 /* Kumulos+Engage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Kumulos+Engage.swift"; sourceTree = "<group>"; };
A13430A01F013D6E00D185FD /* Kumulos+Crash.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "Kumulos+Crash.swift"; path = "Sources/Kumulos+Crash.swift"; sourceTree = "<group>"; };
A13430B31F014C1500D185FD /* KSCrash.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = KSCrash.framework; path = "../Library/Developer/Xcode/DerivedData/KumulosSDK-aqatglguuyycorbdiyjxlgliawoj/Build/Products/Debug-iphoneos/KSCrash.framework"; sourceTree = "<group>"; };
A13430B61F014E5D00D185FD /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -117,6 +119,7 @@
A16790831E49DCDE00C276AF /* PushChannel.swift */,
129372082006355E00EBC8F4 /* Kumulos+Analytics.swift */,
1293720A2006378D00EBC8F4 /* AnalyticsHelper.swift */,
6FE6B5752051550E0025CBC0 /* Kumulos+Engage.swift */,
);
name = Sources;
sourceTree = "<group>";
Expand Down Expand Up @@ -220,6 +223,7 @@
buildActionMask = 2147483647;
files = (
129372072006353600EBC8F4 /* KAnalyticsModel.xcdatamodeld in Sources */,
6FE6B5762051550E0025CBC0 /* Kumulos+Engage.swift in Sources */,
127B22E01D9BE59E00D9C94B /* Kumulos+Stats.swift in Sources */,
127B22D91D9BE59E00D9C94B /* CwlSysctl.swift in Sources */,
127B22DF1D9BE59E00D9C94B /* Kumulos+Push.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion KumulosSdkSwift.podspec
@@ -1,7 +1,7 @@

Pod::Spec.new do |s|
s.name = "KumulosSdkSwift"
s.version = "2.1.1"
s.version = "2.2.0"
s.license = "MIT"
s.summary = "Official Swift SDK for integrating Kumulos services with your mobile apps"
s.homepage = "https://github.com/Kumulos/KumulosSdkSwift"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -9,7 +9,7 @@ Select an installation method below to get started.
Add the following line to your app's target in your `Podfile`:

```
pod 'KumulosSdkSwift', '~> 2.1'
pod 'KumulosSdkSwift', '~> 2.2'
```

Run `pod install` to install your dependencies.
Expand All @@ -19,7 +19,7 @@ Run `pod install` to install your dependencies.
Add the following line to your `Cartfile`:

```
github "Kumulos/KumulosSdkSwift" ~> 2.1
github "Kumulos/KumulosSdkSwift" ~> 2.2
```

Run `carthage update` to install your dependencies then follow the [Carthage integration steps](https://github.com/Carthage/Carthage#getting-started) to link the framework with your project.
Expand Down
17 changes: 11 additions & 6 deletions Sources/AnalyticsHelper.swift
Expand Up @@ -112,12 +112,11 @@ class AnalyticsHelper {
}

// MARK: Event Tracking

func trackEvent(eventType: String, properties: [String:Any]?) {
trackEvent(eventType: eventType, atTime: Date(), properties: properties)
func trackEvent(eventType: String, properties: [String:Any]?, immediateFlush: Bool = false) {
trackEvent(eventType: eventType, atTime: Date(), properties: properties, immediateFlush: immediateFlush)
}

func trackEvent(eventType: String, atTime: Date, properties: [String:Any]?, asynchronously : Bool = true) {
func trackEvent(eventType: String, atTime: Date, properties: [String:Any]?, asynchronously : Bool = true, immediateFlush: Bool = false) {
if eventType == "" || (properties != nil && !JSONSerialization.isValidJSONObject(properties as Any)) {
print("Ignoring invalid event with empty type or non-serializable properties")
return
Expand Down Expand Up @@ -151,6 +150,12 @@ class AnalyticsHelper {

do {
try context.save()

if (immediateFlush) {
DispatchQueue.global(qos: .background).async {
self.syncEvents()
}
}
}
catch {
print("Failed to record event")
Expand Down Expand Up @@ -259,7 +264,7 @@ class AnalyticsHelper {

@objc private func appBecameActive() {
if startNewSession {
trackEvent(eventType: "k.fg", properties: nil)
trackEvent(eventType: KumulosEvent.STATS_FOREGROUND.rawValue, properties: nil)
startNewSession = false
return
}
Expand Down Expand Up @@ -299,7 +304,7 @@ class AnalyticsHelper {
startNewSession = true
sessionIdleTimer = nil

trackEvent(eventType: "k.bg", atTime: becameInactiveAt!, properties: nil, asynchronously: false)
trackEvent(eventType: KumulosEvent.STATS_BACKGROUND.rawValue, atTime: becameInactiveAt!, properties: nil, asynchronously: false)
becameInactiveAt = nil

DispatchQueue.global(qos: .background).async {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Info.plist
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>2.1.1</string>
<string>2.2.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
9 changes: 6 additions & 3 deletions Sources/Kumulos+Analytics.swift
Expand Up @@ -9,6 +9,10 @@ import Foundation

public extension Kumulos {

internal static func trackEvent(eventType: KumulosEvent, properties: [String:Any]?, immediateFlush: Bool = false) {
getInstance().analyticsHelper?.trackEvent(eventType: eventType.rawValue, properties: properties, immediateFlush: immediateFlush)
}

/**
Logs an analytics event to the local database
Expand All @@ -17,7 +21,7 @@ public extension Kumulos {
- properties: Optional meta-data about the event
*/
public static func trackEvent(eventType: String, properties: [String:Any]?) {
getInstance().analyticsHelper?.trackEvent(eventType: eventType, properties: properties)
getInstance().analyticsHelper?.trackEvent(eventType: eventType, properties: properties, immediateFlush: false)
}

/**
Expand All @@ -33,8 +37,7 @@ public extension Kumulos {
}

let params = ["id": userIdentifier]
let url = "\(getInstance().baseStatsUrl)app-installs/\(Kumulos.installId)/user-id"
_ = getInstance().makeJsonNetworkRequest(.put, url: url, parameters: params as [String : AnyObject])
Kumulos.trackEvent(eventType: KumulosEvent.STATS_ASSOCIATE_USER, properties: params as [String : AnyObject], immediateFlush: true)
}

}
9 changes: 3 additions & 6 deletions Sources/Kumulos+Push.swift
Expand Up @@ -53,9 +53,8 @@ public extension Kumulos{
let iosTokenType = getTokenType()

let parameters = ["token" : token, "type" : sharedInstance.pushNotificationDeviceType, "iosTokenType" : iosTokenType] as [String : Any]
let url = "\(sharedInstance.basePushUrl)app-installs/\(Kumulos.installId)/push-token"

_ = sharedInstance.makeNetworkRequest(.put, url: url, parameters: parameters as [String : AnyObject])

Kumulos.trackEvent(eventType: KumulosEvent.PUSH_DEVICE_REGISTER, properties: parameters as [String : AnyObject], immediateFlush: true)
}

/**
Expand All @@ -68,9 +67,7 @@ public extension Kumulos{
if let custom = notification["custom"] as? [String:AnyObject], let id = custom["i"]
{
let parameters = ["id" : id]
let url = "\(sharedInstance.basePushUrl)app-installs/\(Kumulos.installId)/opens"

_ = sharedInstance.makeNetworkRequest(.post, url: url, parameters: parameters)
Kumulos.trackEvent(eventType: KumulosEvent.PUSH_OPEN_TRACK, properties: parameters)
}
}

Expand Down
16 changes: 1 addition & 15 deletions Sources/Kumulos+Stats.swift
Expand Up @@ -5,7 +5,6 @@

import Foundation
import Alamofire
import CoreLocation

#if os(iOS) || os(watchOS) || os(tvOS)
import UIKit
Expand Down Expand Up @@ -61,17 +60,6 @@ struct Platform {

public extension Kumulos{

public static func sendLocationUpdate(location: CLLocation) {
let url = "\(sharedInstance.baseStatsUrl)app-installs/\(Kumulos.installId)/location"

let parameters = [
"lat" : location.coordinate.latitude,
"lng" : location.coordinate.longitude
]

_ = sharedInstance.makeNetworkRequest(.put, url: url, parameters: parameters as [String: AnyObject])
}

internal func sendDeviceInformation() {

var target = TargetType.targetTypeRelease
Expand Down Expand Up @@ -137,9 +125,7 @@ public extension Kumulos{
"device" : device
]

let url = "\(self.baseStatsUrl)app-installs/\(Kumulos.installId)"

_ = self.makeNetworkRequest(.put, url: url, parameters: finalParameters as [String : AnyObject]?)
Kumulos.trackEvent(eventType: KumulosEvent.STATS_CALL_HOME.rawValue, properties: finalParameters)
}

}
Expand Down
11 changes: 11 additions & 0 deletions Sources/Kumulos.swift
Expand Up @@ -15,6 +15,17 @@ protocol KumulosDelegate: class {
func didFail(_ kumulos: Kumulos, operation: KSAPIOperation, error: NSError?)
}

internal enum KumulosEvent : String {
case STATS_FOREGROUND = "k.fg"
case STATS_BACKGROUND = "k.bg"
case STATS_CALL_HOME = "k.stats.installTracked"
case STATS_ASSOCIATE_USER = "k.stats.userAssociated"
case PUSH_DEVICE_REGISTER = "k.push.deviceRegistered"
case PUSH_OPEN_TRACK = "k.push.opened"
case ENGAGE_BEACON_ENTERED_PROXIMITY = "k.engage.beaconEnteredProximity"
case ENGAGE_LOCATION_UPDATED = "k.engage.locationUpdated"
}

// MARK: class
open class Kumulos {

Expand Down

0 comments on commit fbfc8cd

Please sign in to comment.