-
Notifications
You must be signed in to change notification settings - Fork 11
/
AmpliSwiftSampleAppApp.swift
116 lines (97 loc) · 4.43 KB
/
AmpliSwiftSampleAppApp.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// AmpliSwiftSampleAppApp.swift
// Shared
//
// Created by Qingzhuo Zhen on 11/12/21.
//
import SwiftUI
import AmplitudeSwift
@main
struct AmpliSwiftSampleAppApp: App {
let persistenceController = PersistenceController.shared
var body: some Scene {
// 'Ampli.instance' is the default instance of Ampli()
//
// When you pull your tracking plan you can use the defaults and call load() without arguments
// This requires connecting your account via `ampli pull` which will set you API key in the generated Ampli SDK
//
// OR Specify a AmpliEnvironment
// ampli.load(LoadOptions(environment: AmpliEnvironment.dev))
//
// OR Provide a specific Amplitude API key
// ampli.load(LoadOptions(client: LoadClientOptions(apiKey: "Custom api key")))
//
// OR Use an existing Amplitude instance
// requires "import AmplitudeSwift"
// let instance = Amplitude(configuration: Configuration(apiKey: "Custom api key", instanceName: "instanceName"))
// ampli.load(LoadOptions(client: LoadClientOptions(instance: instance)))
//
// For testing you can disable ampli
// ampli.load(LoadOptions(disabled: ProcessInfo.processInfo.environment["IS_TESTING"] == "true" ? true: false))
//
// Make as many Ampli instances as you want
// let ampli2 = Ampli()
// ampli2.load(LoadOptions(client: LoadClientOptions(apiKey: "api-key-2")))
let apiKey = ProcessInfo.processInfo.environment["AMPLITUDE_API_KEY"] ?? "test-api-key"
let ampli = Ampli.instance
// Load
ampli.load(LoadOptions(client: LoadClientOptions(apiKey: apiKey)))
ampli.client.add(plugin: LoggingPlugin())
// Identify
ampli.identify("ampli-swift-user", Identify(requiredNumber: 22.0, optionalArray: ["optional string"]))
// Set group
ampli.client.setGroup(groupType: "ampli group type", groupName: "ampli swift group")
// GroupIdentify
let groupProperties = ["requiredBoolean": true]
ampli.client.groupIdentify(groupType: "ampli group type", groupName: "ampli swift group", groupProperties: groupProperties)
// Track events with dedicated event methods
ampli.eventNoProperties()
ampli.eventMaxIntForTest(intMax10: 20)
ampli.eventWithConstTypes()
// Track using event instances and ampli.track(event, options, extra)
let eventWithAllProperties = EventWithAllProperties(
requiredArray: ["array element 1", "array element 2"],
requiredBoolean: true,
requiredEnum: EventWithAllProperties.RequiredEnum.enum1,
requiredInteger: 10,
requiredNumber: 2.0,
requiredString: "required string",
optionalString: nil
)
ampli.track(eventWithAllProperties)
ampli.eventObjectTypes(
requiredObject: ["prop1": 3, "prop2": "abc"],
requiredObjectArray: [1, true, "string", ["prop1": 23, "prop2": "xyz"]]
)
ampli.eventWithEnumTypes(
requiredEnum: EventWithEnumTypes.RequiredEnum.requiredEnum2
)
ampli.eventWithOptionalArrayTypes(
optionalBooleanArray: [false, true]
)
ampli.eventWithOptionalProperties(
optionalString: "optional string"
)
ampli.eventWithDifferentCasingTypes(
enumWithSpace: EventWithDifferentCasingTypes.EnumWithSpace.enumWithSpace,
enumSnakeCase: EventWithDifferentCasingTypes.EnumSnakeCase.enumSnakeCase,
enumCamelCase: EventWithDifferentCasingTypes.EnumCamelCase.enumCamelCase,
enumPascalCase: EventWithDifferentCasingTypes.EnumPascalCase.enumPascalCase,
propertyWithSpace: "property with space",
propertyWithSnakeCase: "property with snake case",
propertyWithCamelCase: "property with camel case",
propertyWithPascalCase: "property with pascal case"
)
ampli.eventWithTemplateProperties(
requiredEventProperty: "event property",
requiredTemplateProperty: "template property",
optionalEventProperty: 1.23
)
ampli.flush()
return WindowGroup {
ContentView()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
TextView()
}
}
}