forked from amplitude/ampli-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AmpliSwiftSampleAppApp.swift
122 lines (104 loc) · 4.58 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
117
118
119
120
121
122
//
// AmpliSwiftSampleAppApp.swift
// Shared
//
// Created by Qingzhuo Zhen on 11/12/21.
//
import SwiftUI
import Amplitude
@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.development)
//
// OR Provide a specific Amplitude API key
// ampli.load(LoadOptions(client: LoadClientOptions(apiKey: "Custom api key"))
//
// OR Use an existing Amplitude instance
// requires "import Amplitude"
// let instance = Amplitude.instance("instanceName");
// instance.initializeApiKey("Custom api key");
// ampli.load(LoadOptions(client: LoadClientOptions(instance: instance)))
//
// For testing you can disable ampli
// ampli.load(LoadOptions(disabled: ENV.IS_TESTING ? true: false))
//
// Make as many Ampli instances as you want
// let ampli2 = new Ampli();
// ampli2.load(LoadOptions(client: LoadClientOptions(apiKey: "api-key-2")))
let apiKey = ProcessInfo.processInfo.environment["AMPLITUDE_API_KEY"];
let ampli = Ampli.instance
let extraDict = ["test" : "extra test"];
// Load
ampli.load(LoadOptions(client: LoadClientOptions(apiKey: apiKey)))
// Add Middleware
let loggingMiddleware = AMPBlockMiddleware { (payload, next) in
// Output event and extra from payload
print(String(format:"[ampli] event=\(payload.event) payload=\(String(describing: payload.extra))"))
// Continue to next middleware
next(payload);
}
ampli.client.addEventMiddleware(loggingMiddleware)
// Identify
ampli.identify("ampli-swift-user", Identify(requiredNumber: 22.0, optionalArray: ["optional string"]))
// Set group
ampli.setGroup("ampli group type", "ampli swift group")
// GroupIdentify
ampli.groupIdentify("ampli group type", "ampli swift group", Group(requiredBoolean: true))
// 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, extra: extraDict)
ampli.eventObjectTypes(
requiredObject: 3,
requiredObjectArray: [1, true, "string"]
)
ampli.eventWithEnumTypes(
requiredEnum: EventWithEnumTypes.RequiredEnum.requiredEnum2
)
ampli.eventWithOptionalArrayTypes(
optionalBooleanArray: [false, true]
)
ampli.eventWithOptionalProperties(
optionalString: "optional string"
)
ampli.eventWithDifferentCasingTypes(
enumCamelCase: EventWithDifferentCasingTypes.EnumCamelCase.enumCamelCase,
enumPascalCase: EventWithDifferentCasingTypes.EnumPascalCase.enumPascalCase,
enumSnakeCase: EventWithDifferentCasingTypes.EnumSnakeCase.enumSnakeCase,
enumWithSpace: EventWithDifferentCasingTypes.EnumWithSpace.enumWithSpace,
propertyWithCamelCase: "property with camel case",
propertyWithPascalCase: "property with pascal case",
propertyWithSnakeCase: "property with snake case",
propertyWithSpace: "property with space"
)
ampli.eventWithTemplateProperties(
requiredEventProperty: "event property",
requiredTemplateProperty: "template property",
optionalEventProperty: 1.23
)
return WindowGroup {
ContentView()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
TextView()
}
}
}