Skip to content

Commit c47cf28

Browse files
authored
feat: add inject traits plugin example (#181)
* feat: add inject traits plugin example * add license and move to plugin examples folder Co-authored-by: Alan Charles <[email protected]>
1 parent 594813f commit c47cf28

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// InjectTraits.swift
3+
4+
// Created by Alan Charles on 12/7/22.
5+
//
6+
7+
// This plugin is NOT SUPPORTED by Segment. It is here merely as an example,
8+
// and for your convenience should you find it useful.
9+
10+
// MIT License
11+
//
12+
// Copyright (c) 2021 Segment
13+
//
14+
// Permission is hereby granted, free of charge, to any person obtaining a copy
15+
// of this software and associated documentation files (the "Software"), to deal
16+
// in the Software without restriction, including without limitation the rights
17+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18+
// copies of the Software, and to permit persons to whom the Software is
19+
// furnished to do so, subject to the following conditions:
20+
//
21+
// The above copyright notice and this permission notice shall be included in all
22+
// copies or substantial portions of the Software.
23+
//
24+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30+
// SOFTWARE.
31+
32+
import Foundation
33+
import Segment
34+
35+
class InjectTraits: Plugin {
36+
let type = PluginType.enrichment
37+
weak var analytics: Analytics? = nil
38+
39+
func execute<T: RawEvent>(event: T?) -> T? {
40+
if event?.type == "identify" {
41+
return event
42+
}
43+
44+
var workingEvent = event
45+
46+
if var context = event?.context?.dictionaryValue {
47+
context[keyPath: "traits"] = analytics?.traits()
48+
49+
workingEvent?.context = try? JSON(context)
50+
}
51+
52+
return workingEvent
53+
}
54+
}

0 commit comments

Comments
 (0)