|
| 1 | +// |
| 2 | +// ExampleDestination.swift |
| 3 | +// ExampleDestination |
| 4 | +// |
| 5 | +// Created by Cody Garvin on 9/13/21. |
| 6 | +// |
| 7 | + |
| 8 | +// NOTE: You can see this plugin in use in the DestinationsExample 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 | + |
| 14 | +// MIT License |
| 15 | +// |
| 16 | +// Copyright (c) 2021 Segment |
| 17 | +// |
| 18 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 19 | +// of this software and associated documentation files (the "Software"), to deal |
| 20 | +// in the Software without restriction, including without limitation the rights |
| 21 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 22 | +// copies of the Software, and to permit persons to whom the Software is |
| 23 | +// furnished to do so, subject to the following conditions: |
| 24 | +// |
| 25 | +// The above copyright notice and this permission notice shall be included in all |
| 26 | +// copies or substantial portions of the Software. |
| 27 | +// |
| 28 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 29 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 30 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 31 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 32 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 33 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 34 | +// SOFTWARE. |
| 35 | + |
| 36 | +import Foundation |
| 37 | +import Segment |
| 38 | +//import ExampleModule // TODO: Import partner SDK module here |
| 39 | + |
| 40 | +/** |
| 41 | + An implementation of the Example Analytics device mode destination as a plugin. |
| 42 | + */ |
| 43 | + |
| 44 | +class ExampleDestination: DestinationPlugin { |
| 45 | + let timeline = Timeline() |
| 46 | + let type = PluginType.destination |
| 47 | + // TODO: Fill this out with your settings key that matches your destination in the Segment App |
| 48 | + let key = "Example" |
| 49 | + var analytics: Analytics? = nil |
| 50 | + |
| 51 | + private var exampleSettings: ExampleSettings? |
| 52 | + |
| 53 | + func update(settings: Settings, type: UpdateType) { |
| 54 | + // Skip if you have a singleton and don't want to keep updating via settings. |
| 55 | + guard type == .initial else { return } |
| 56 | + |
| 57 | + // Grab the settings and assign them for potential later usage. |
| 58 | + // Note: Since integrationSettings is generic, strongly type the variable. |
| 59 | + guard let tempSettings: ExampleSettings = settings.integrationSettings(forPlugin: self) else { return } |
| 60 | + exampleSettings = tempSettings |
| 61 | + |
| 62 | + // TODO: initialize partner SDK here |
| 63 | + } |
| 64 | + |
| 65 | + func identify(event: IdentifyEvent) -> IdentifyEvent? { |
| 66 | + |
| 67 | + if let _ = event.traits?.dictionaryValue { |
| 68 | + // TODO: Do something with traits if they exist |
| 69 | + } |
| 70 | + |
| 71 | + // TODO: Do something with userId & traits in partner SDK |
| 72 | + |
| 73 | + return event |
| 74 | + } |
| 75 | + |
| 76 | + func track(event: TrackEvent) -> TrackEvent? { |
| 77 | + |
| 78 | + var returnEvent = event |
| 79 | + |
| 80 | + // !!!: Sample of how to convert property keys |
| 81 | + if let mappedProperties = try? event.properties?.mapTransform(ExampleDestination.eventNameMap, |
| 82 | + valueTransform: ExampleDestination.eventValueConversion) { |
| 83 | + returnEvent.properties = mappedProperties |
| 84 | + } |
| 85 | + |
| 86 | + // TODO: Do something with event & properties in partner SDK from returnEvent |
| 87 | + |
| 88 | + return returnEvent |
| 89 | + } |
| 90 | + |
| 91 | + func screen(event: ScreenEvent) -> ScreenEvent? { |
| 92 | + |
| 93 | + if let _ = event.properties?.dictionaryValue { |
| 94 | + // TODO: Do something with properties if they exist |
| 95 | + } |
| 96 | + |
| 97 | + // TODO: Do something with name, category & properties in partner SDK |
| 98 | + |
| 99 | + return event |
| 100 | + } |
| 101 | + |
| 102 | + func group(event: GroupEvent) -> GroupEvent? { |
| 103 | + |
| 104 | + if let _ = event.traits?.dictionaryValue { |
| 105 | + // TODO: Do something with traits if they exist |
| 106 | + } |
| 107 | + |
| 108 | + // TODO: Do something with groupId & traits in partner SDK |
| 109 | + |
| 110 | + return event |
| 111 | + } |
| 112 | + |
| 113 | + func alias(event: AliasEvent) -> AliasEvent? { |
| 114 | + |
| 115 | + // TODO: Do something with previousId & userId in partner SDK |
| 116 | + |
| 117 | + return event |
| 118 | + } |
| 119 | + |
| 120 | + func reset() { |
| 121 | + // TODO: Do something with resetting partner SDK |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// Example of what settings may look like. |
| 126 | +private struct ExampleSettings: Codable { |
| 127 | + let apiKey: String |
| 128 | + let configB: Int? |
| 129 | + let configC: Bool? |
| 130 | +} |
| 131 | + |
| 132 | +// Rules for converting keys and values to the proper formats that bridge |
| 133 | +// from Segment to the Partner SDK. These are only examples. |
| 134 | +private extension ExampleDestination { |
| 135 | + |
| 136 | + static var eventNameMap = ["ADD_TO_CART": "Product Added", |
| 137 | + "PRODUCT_TAPPED": "Product Tapped"] |
| 138 | + |
| 139 | + static var eventValueConversion: ((_ key: String, _ value: Any) -> Any) = { (key, value) in |
| 140 | + if let valueString = value as? String { |
| 141 | + return valueString |
| 142 | + .replacingOccurrences(of: "-", with: "_") |
| 143 | + .replacingOccurrences(of: " ", with: "_") |
| 144 | + } else { |
| 145 | + return value |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments