Skip to content

Commit 5c35fc5

Browse files
committed
sdk changes:
- add section tracking
1 parent 8aba5da commit 5c35fc5

File tree

9 files changed

+95
-40
lines changed

9 files changed

+95
-40
lines changed

Example/Example/AppDelegate.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1818
// Initialize UserReport SDK
1919
let user = User()
2020
user.email = "[email protected]"
21-
UserReport.configure(sakId: "ios-playground", mediaId: "df5be674-b6a8-4bb8-8f44-4c8229a01bc2", user: user, userSettings: settings)
22-
21+
UserReport.configure(sakId: "audienceproject", mediaId: "3402b774-b7a8-448c-997a-ef6cd59efc41", user: user, userSettings: settings)
2322

2423
return true
2524
}

Example/Example/Base.lproj/Main.storyboard

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="8ss-6d-3qK">
3-
<device id="retina4_7" orientation="portrait">
4-
<adaptation id="fullscreen"/>
5-
</device>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="15705" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="8ss-6d-3qK">
3+
<device id="retina4_7" orientation="portrait" appearance="light"/>
64
<dependencies>
75
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
6+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15706"/>
97
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
108
</dependencies>
119
<scenes>
@@ -14,7 +12,7 @@
1412
<objects>
1513
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="8ss-6d-3qK" sceneMemberID="viewController">
1614
<navigationBar key="navigationBar" contentMode="scaleToFill" id="RNO-OA-Xgd">
17-
<rect key="frame" x="0.0" y="20" width="375" height="44"/>
15+
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
1816
<autoresizingMask key="autoresizingMask"/>
1917
</navigationBar>
2018
<connections>
@@ -38,10 +36,10 @@
3836
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
3937
<subviews>
4038
<scrollView opaque="NO" multipleTouchEnabled="YES" contentMode="scaleToFill" showsHorizontalScrollIndicator="NO" showsVerticalScrollIndicator="NO" translatesAutoresizingMaskIntoConstraints="NO" id="IRW-3D-fBh">
41-
<rect key="frame" x="20" y="64" width="335" height="703"/>
39+
<rect key="frame" x="20" y="44" width="335" height="723"/>
4240
<subviews>
4341
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="4AU-R4-f4z">
44-
<rect key="frame" x="75" y="625.5" width="185" height="40"/>
42+
<rect key="frame" x="75" y="675.5" width="185" height="40"/>
4543
<constraints>
4644
<constraint firstAttribute="height" constant="40" id="8qe-EP-nDe"/>
4745
<constraint firstAttribute="width" relation="greaterThanOrEqual" constant="185" id="hBW-nz-kAX"/>
@@ -55,7 +53,7 @@
5553
</connections>
5654
</button>
5755
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="20" translatesAutoresizingMaskIntoConstraints="NO" id="KGm-O9-fCG">
58-
<rect key="frame" x="0.0" y="20" width="335" height="585.5"/>
56+
<rect key="frame" x="0.0" y="20" width="335" height="635.5"/>
5957
<subviews>
6058
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="ETb-9h-PbN" customClass="ShadowView" customModule="Example" customModuleProvider="target">
6159
<rect key="frame" x="0.0" y="0.0" width="335" height="238"/>
@@ -147,10 +145,10 @@
147145
</userDefinedRuntimeAttributes>
148146
</view>
149147
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="9zP-9M-eVM" customClass="ShadowView" customModule="Example" customModuleProvider="target">
150-
<rect key="frame" x="0.0" y="258" width="335" height="327.5"/>
148+
<rect key="frame" x="0.0" y="258" width="335" height="377.5"/>
151149
<subviews>
152150
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="10" translatesAutoresizingMaskIntoConstraints="NO" id="Lya-P5-EyQ">
153-
<rect key="frame" x="10" y="10" width="315" height="307.5"/>
151+
<rect key="frame" x="10" y="10" width="315" height="357.5"/>
154152
<subviews>
155153
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="a9O-S7-826">
156154
<rect key="frame" x="0.0" y="0.0" width="315" height="43.5"/>
@@ -422,20 +420,45 @@
422420
<constraint firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="9wI-mj-Pt6" secondAttribute="trailing" id="ucl-9T-yJQ"/>
423421
</constraints>
424422
</view>
425-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="0yZ-dm-zJR">
423+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="soN-Vi-FIy">
426424
<rect key="frame" x="0.0" y="267.5" width="315" height="40"/>
427425
<subviews>
428-
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="S0k-Ee-2oP">
426+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="T4f-pB-Leq">
429427
<rect key="frame" x="0.0" y="0.0" width="125" height="40"/>
430428
<constraints>
431-
<constraint firstAttribute="width" relation="greaterThanOrEqual" constant="125" id="I02-YT-Q5M"/>
432-
<constraint firstAttribute="height" constant="40" id="aHm-4Y-NCi"/>
429+
<constraint firstAttribute="width" relation="greaterThanOrEqual" constant="125" id="NUO-br-Hj0"/>
430+
<constraint firstAttribute="height" constant="40" id="psP-Mk-VEW"/>
433431
</constraints>
434432
<state key="normal" title="trackScreen()" backgroundImage="bg_button_normal">
435433
<color key="titleColor" red="0.56470588239999997" green="0.51764705879999995" blue="1" alpha="1" colorSpace="calibratedRGB"/>
436434
</state>
437435
<connections>
438-
<action selector="trackScreen:" destination="BYZ-38-t0r" eventType="touchUpInside" id="pTq-Cy-VGr"/>
436+
<action selector="trackScreen:" destination="BYZ-38-t0r" eventType="touchUpInside" id="JsD-YK-oHi"/>
437+
</connections>
438+
</button>
439+
</subviews>
440+
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
441+
<constraints>
442+
<constraint firstItem="T4f-pB-Leq" firstAttribute="leading" secondItem="soN-Vi-FIy" secondAttribute="leading" id="8c3-pc-nlp"/>
443+
<constraint firstItem="T4f-pB-Leq" firstAttribute="top" secondItem="soN-Vi-FIy" secondAttribute="top" id="cgM-Db-BI9"/>
444+
<constraint firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="T4f-pB-Leq" secondAttribute="trailing" id="hjv-6p-m9O"/>
445+
<constraint firstAttribute="bottom" secondItem="T4f-pB-Leq" secondAttribute="bottom" id="w48-Vp-BPB"/>
446+
</constraints>
447+
</view>
448+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="0yZ-dm-zJR">
449+
<rect key="frame" x="0.0" y="317.5" width="315" height="40"/>
450+
<subviews>
451+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="S0k-Ee-2oP">
452+
<rect key="frame" x="0.0" y="0.0" width="170" height="40"/>
453+
<constraints>
454+
<constraint firstAttribute="width" relation="greaterThanOrEqual" constant="170" id="I02-YT-Q5M"/>
455+
<constraint firstAttribute="height" constant="40" id="aHm-4Y-NCi"/>
456+
</constraints>
457+
<state key="normal" title="trackSectionScreen()" backgroundImage="bg_button_normal">
458+
<color key="titleColor" red="0.56470588239999997" green="0.51764705879999995" blue="1" alpha="1" colorSpace="calibratedRGB"/>
459+
</state>
460+
<connections>
461+
<action selector="trackSessionScreen:" destination="BYZ-38-t0r" eventType="touchUpInside" id="nUW-kY-pPg"/>
439462
</connections>
440463
</button>
441464
</subviews>

Example/Example/ViewController.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,13 @@ class ViewController: UserReportViewController {
133133
}
134134

135135
@IBAction func trackScreen(_ sender: Any) {
136-
UserReport.shared?.trackScreen()
136+
UserReport.shared?.trackScreenView()
137137
self.updateSessionInfo()
138138
}
139139

140+
@IBAction func trackSessionScreen(_ sender: Any) {
141+
UserReport.shared?.trackSectionScreenView("b556da9f-9443-44ab-aa48-63d579318b69")
142+
self.updateSessionInfo()
143+
}
140144
}
141145

Example_Objective-C/Example_Objective-C/ViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ - (IBAction)selectFullscreenDisplayMode:(id)sender {
119119
}
120120

121121
- (IBAction)trackScreen:(id)sender {
122-
[[UserReport shared] trackScreen];
122+
[[UserReport shared] trackScreenView];
123123
[self updateSessionInfo];
124124
}
125125

UserReport/UserReport/Models/MediaSettings.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ internal struct MediaSettings {
1717

1818
/// Rules after which the survey will be displayed on the screen
1919
var settings: Settings = Settings()
20+
21+
var sections: Dictionary<String, String>?
2022
}
2123

2224
/**
@@ -55,6 +57,10 @@ extension MediaSettings: Serialization {
5557
if let sessionNSecondsLength = dict["sessionNSecondsLength"] as? TimeInterval {
5658
self.settings.sessionNSecondsLength = sessionNSecondsLength
5759
}
60+
61+
if let sections = dict["sections"] as? Dictionary<String, String> {
62+
self.sections = sections
63+
}
5864
}
5965

6066
}

UserReport/UserReport/Network/Network.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,11 @@ internal class Network {
7979
self.sendRequest(httpMethod: HTTPMethod.GET, url: url, body: nil, completion: completion)
8080
}
8181

82-
func audiences(info: Info, completion: @escaping ((Result<Empty>) -> Void)) {
82+
func trackScreenView(info: Info, tCode: String, completion: @escaping ((Result<Empty>) -> Void)) {
8383
//https://visitanalytics.userreport.com/hit.gif?t=[kitTcode]&rnd=%RANDOM%&d=IDFA&med=app_name
8484
let appName = Bundle.main.infoDictionary![kCFBundleNameKey as String] as! String
8585

86-
guard let kitTcode = info.mediaSettings?.kitTcode else {
87-
return
88-
}
89-
90-
let tCode = "t=\(kitTcode)&"
86+
let tCode = "t=\(tCode)&"
9187
let random = arc4random_uniform(UInt32.max)
9288
let idfa = info.user?.idfa ?? ""
9389
let url = URL(string: "\(self.server.audiences)/hit.gif?\(tCode)rnd=\(random)&d=\(idfa)&med=\(appName)")

UserReport/UserReport/Session/Session.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public class Session: NSObject {
9393
/**
9494
* Tracking screen view
9595
*/
96-
internal func trackScreenView() {
96+
internal func updateScreenViewed() {
9797
self.screenView += 1
9898
self.totalScreenView += 1
9999
self.lastViewScreenDate = Date()

UserReport/UserReport/UI/UserReportViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ open class UserReportViewController: UIViewController {
2020
super.viewDidAppear(animated)
2121

2222
// Tracking screen view
23-
UserReport.shared?.trackScreen()
23+
UserReport.shared?.trackScreenView()
2424
}
2525

2626
}

UserReport/UserReport/UserReport.swift

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,25 @@ private var sharedInstance: UserReport?
113113
/**
114114
* Tracking screen view
115115
*/
116-
@objc public func trackScreen() {
116+
@objc public func trackScreenView() {
117117

118-
// Track audience every screen view instead first scren view because we already tracked audience when initialize SDK
118+
// Track audience every screen view instead first screen view because we already tracked audience when initialize SDK
119119
if self.session.screenView != 0 {
120-
self.trackAudience()
120+
self.sendTrackScreenView()
121121
}
122122

123123
// Update session `screenView` and `totalScreenView` values
124-
self.session.trackScreenView()
124+
self.session.updateScreenViewed()
125+
}
126+
127+
/**
128+
* Tracking section screen view
129+
*/
130+
@objc public func trackSectionScreenView(_ sectionId: String) {
131+
self.sendTrackSectionScreenView(sectionId)
125132

126-
// Add info to logger
127-
self.logger.log("Viewed screen", level: .info)
133+
// Update session `screenView` and `totalScreenView` values
134+
self.session.updateScreenViewed()
128135
}
129136

130137
/**
@@ -204,14 +211,12 @@ private var sharedInstance: UserReport?
204211
if let usrSettings = userSettings {
205212
self.session.updateSettings(usrSettings)
206213
}
207-
208-
self.logger.log("Settings: \(result.value!)", level: .debug)
209214
case .failure(let error):
210215
self.logger.log("Failed get config. Error: \(error.localizedDescription)", level: .error)
211216
}
212217

213218
self.logVisit()
214-
self.trackAudience()
219+
self.trackScreenView()
215220
}
216221
}
217222

@@ -232,13 +237,35 @@ private var sharedInstance: UserReport?
232237
/**
233238
* Tracking audience measurement for Kits
234239
*/
235-
private func trackAudience() {
236-
self.network.audiences(info: self.info) { (result) in
240+
private func sendTrackScreenView() {
241+
guard let tCode = info.mediaSettings?.kitTcode else {
242+
return
243+
}
244+
245+
self.network.trackScreenView(info: self.info, tCode: tCode) { (result) in
246+
switch (result) {
247+
case .success:
248+
self.logger.log("Viewed screen", level: .info)
249+
break
250+
case .failure(let error):
251+
self.logger.log("Can't track screenview. Error: \(error.localizedDescription)", level: .error)
252+
}
253+
}
254+
}
255+
256+
private func sendTrackSectionScreenView(_ sectionId: String) {
257+
guard let tCode = info.mediaSettings?.sections?[sectionId] else {
258+
self.logger.log("Can't find section in media. Section Id: \(sectionId)", level: .error)
259+
return
260+
}
261+
262+
self.network.trackScreenView(info: self.info, tCode: tCode) { (result) in
237263
switch (result) {
238264
case .success:
265+
self.logger.log("Viewed section screen", level: .info)
239266
break
240267
case .failure(let error):
241-
self.logger.log("Can't track audience. Error: \(error.localizedDescription)", level: .error)
268+
self.logger.log("Can't track section screenview. Error: \(error.localizedDescription)", level: .error)
242269
}
243270
}
244271
}

0 commit comments

Comments
 (0)