Skip to content

Commit d39bd65

Browse files
bsneedBrandon Sneed
and
Brandon Sneed
authored
Example plugins for more detailed cellular info (#139)
Co-authored-by: Brandon Sneed <[email protected]>
1 parent 0741945 commit d39bd65

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

Examples/apps/SegmentUIKitExample/SegmentUIKitExample.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
4602278C261F860100A9E913 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4602278A261F860100A9E913 /* Main.storyboard */; };
1414
4602278E261F860300A9E913 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4602278D261F860300A9E913 /* Assets.xcassets */; };
1515
46022791261F860300A9E913 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4602278F261F860300A9E913 /* LaunchScreen.storyboard */; };
16+
463B27772805F4ED0090B83E /* CellularCarrier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 463B27762805F4ED0090B83E /* CellularCarrier.swift */; };
1617
46E3834C26582D9E00BA2502 /* UIKitScreenTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E3834826582D9E00BA2502 /* UIKitScreenTracking.swift */; };
1718
46E3834D26582D9E00BA2502 /* ConsentTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E3834926582D9E00BA2502 /* ConsentTracking.swift */; };
1819
46E3834E26582D9E00BA2502 /* IDFACollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E3834A26582D9E00BA2502 /* IDFACollection.swift */; };
@@ -33,6 +34,7 @@
3334
46022790261F860300A9E913 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
3435
46022792261F860300A9E913 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3536
460227BC2620C02B00A9E913 /* SegmentUIKitExample-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SegmentUIKitExample-Bridging-Header.h"; sourceTree = "<group>"; };
37+
463B27762805F4ED0090B83E /* CellularCarrier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CellularCarrier.swift; sourceTree = "<group>"; };
3638
469F7AFA265D8D890038E773 /* SegmentUIKitExample.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SegmentUIKitExample.entitlements; sourceTree = "<group>"; };
3739
46E3834826582D9E00BA2502 /* UIKitScreenTracking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIKitScreenTracking.swift; sourceTree = "<group>"; };
3840
46E3834926582D9E00BA2502 /* ConsentTracking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConsentTracking.swift; sourceTree = "<group>"; };
@@ -94,6 +96,7 @@
9496
isa = PBXGroup;
9597
children = (
9698
46E3834826582D9E00BA2502 /* UIKitScreenTracking.swift */,
99+
463B27762805F4ED0090B83E /* CellularCarrier.swift */,
97100
46E73DA226F531320021042C /* NotificationTracking.swift */,
98101
46E3834926582D9E00BA2502 /* ConsentTracking.swift */,
99102
46E3834A26582D9E00BA2502 /* IDFACollection.swift */,
@@ -206,6 +209,7 @@
206209
46E73DA326F531320021042C /* NotificationTracking.swift in Sources */,
207210
46022787261F860100A9E913 /* SceneDelegate.swift in Sources */,
208211
46E3834D26582D9E00BA2502 /* ConsentTracking.swift in Sources */,
212+
463B27772805F4ED0090B83E /* CellularCarrier.swift in Sources */,
209213
);
210214
runOnlyForDeploymentPostprocessing = 0;
211215
};

Examples/apps/SegmentUIKitExample/SegmentUIKitExample/AppDelegate.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1818
Analytics.main.add(plugin: IDFACollection())
1919
Analytics.main.add(plugin: UIKitScreenTracking())
2020
Analytics.main.add(plugin: NotificationTracking())
21+
Analytics.main.add(plugin: CellularCarrier())
2122

2223
Analytics.support.add(plugin: ConsoleLogger(name: "support"))
2324
Analytics.support.add(plugin: ConsentTracking())
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//
2+
// CellularCarrier.swift
3+
//
4+
//
5+
// Created by Brandon Sneed on 4/12/22.
6+
//
7+
8+
// NOTE: You can see this plugin in use in the SwiftUIKitExample application.
9+
//
10+
// This plugin is NOT SUPPORTED by Segment. It is here merely as an example,
11+
// and for your convenience should you find it useful.
12+
13+
// MIT License
14+
//
15+
// Copyright (c) 2021 Segment
16+
//
17+
// Permission is hereby granted, free of charge, to any person obtaining a copy
18+
// of this software and associated documentation files (the "Software"), to deal
19+
// in the Software without restriction, including without limitation the rights
20+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21+
// copies of the Software, and to permit persons to whom the Software is
22+
// furnished to do so, subject to the following conditions:
23+
//
24+
// The above copyright notice and this permission notice shall be included in all
25+
// copies or substantial portions of the Software.
26+
//
27+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33+
// SOFTWARE.
34+
35+
import Foundation
36+
import Segment
37+
import CoreTelephony
38+
39+
/**
40+
An example plugin to retrieve cellular information.
41+
42+
This plugin will add all carrier information to the event's context.network object if cellular
43+
is currently in use. Example contents:
44+
45+
{
46+
"home": "T-Mobile",
47+
"roaming": "AT&T",
48+
"secondary": "Verizon",
49+
}
50+
51+
*/
52+
class CellularCarrier: Plugin {
53+
var type: PluginType = .enrichment
54+
55+
var analytics: Analytics?
56+
57+
func execute<T: RawEvent>(event: T?) -> T? {
58+
guard var workingEvent = event else { return event }
59+
60+
if let isCellular: Bool = workingEvent.context?[keyPath: "network"],
61+
isCellular,
62+
let carriers = self.carriers
63+
{
64+
workingEvent.context?[keyPath: "network.carriers"] = carriers
65+
}
66+
67+
return workingEvent
68+
}
69+
70+
// done as a compute-once stored property; your use case may be different.
71+
var carriers: [String: String]? = {
72+
let info = CTTelephonyNetworkInfo()
73+
if let providers = info.serviceSubscriberCellularProviders {
74+
var results = [String: String]()
75+
for (key, value) in providers {
76+
if let carrier = value.carrierName, !carrier.isEmpty {
77+
results[key] = value.carrierName
78+
}
79+
}
80+
if !results.isEmpty {
81+
return results
82+
}
83+
}
84+
return nil
85+
}()
86+
}
87+
88+
89+
/**
90+
An example plugin to retrieve cellular information.
91+
92+
This plugin will add primary ("home") carrier information to the event's context.network object if cellular
93+
is currently in use. This mimics the operation of the analytics-ios SDK.
94+
*/
95+
class PrimaryCellularCarrier: Plugin {
96+
var type: PluginType = .enrichment
97+
98+
var analytics: Analytics?
99+
100+
func execute<T: RawEvent>(event: T?) -> T? {
101+
guard var workingEvent = event else { return event }
102+
103+
if let isCellular: Bool = workingEvent.context?[keyPath: "network"],
104+
isCellular,
105+
let carrier = self.carrier
106+
{
107+
workingEvent.context?[keyPath: "network.carriers"] = carrier
108+
}
109+
110+
return workingEvent
111+
}
112+
113+
// done as a compute-once stored property; your use case may be different.
114+
var carrier: String? = {
115+
let info = CTTelephonyNetworkInfo()
116+
if let providers = info.serviceSubscriberCellularProviders {
117+
let primary = providers["home"]
118+
if let carrier = primary?.carrierName, !carrier.isEmpty {
119+
return carrier
120+
}
121+
}
122+
return nil
123+
}()
124+
}
125+

0 commit comments

Comments
 (0)