forked from snowplow/snowplow-java-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTracker.java
More file actions
196 lines (168 loc) · 5.54 KB
/
Copy pathTracker.java
File metadata and controls
196 lines (168 loc) · 5.54 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright (c) 2014-2020 Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package com.snowplowanalytics.snowplow.tracker;
import com.google.common.base.Preconditions;
import com.snowplowanalytics.snowplow.tracker.emitter.Emitter;
import com.snowplowanalytics.snowplow.tracker.events.*;
import com.snowplowanalytics.snowplow.tracker.payload.TrackerEvent;
import com.snowplowanalytics.snowplow.tracker.payload.TrackerParameters;
public class Tracker {
private Emitter emitter;
private Subject subject;
private final TrackerParameters parameters;
/**
* Creates a new Snowplow Tracker.
*
* @param builder The builder that constructs a tracker
*/
private Tracker(TrackerBuilder builder) {
// Precondition checks
Preconditions.checkNotNull(builder.emitter);
Preconditions.checkNotNull(builder.namespace);
Preconditions.checkNotNull(builder.appId);
Preconditions.checkArgument(!builder.namespace.isEmpty(), "namespace cannot be empty");
Preconditions.checkArgument(!builder.appId.isEmpty(), "appId cannot be empty");
this.parameters = new TrackerParameters(builder.appId, builder.platform, builder.namespace, Version.TRACKER, builder.base64Encoded);
this.emitter = builder.emitter;
this.subject = builder.subject;
}
/**
* Builder for the Tracker
*/
public static class TrackerBuilder {
private final Emitter emitter; // Required
private final String namespace; // Required
private final String appId; // Required
private Subject subject = null; // Optional
private DevicePlatform platform = DevicePlatform.ServerSideApp; // Optional
private boolean base64Encoded = true; // Optional
/**
* @param emitter Emitter to which events will be sent
* @param namespace Identifier for the Tracker instance
* @param appId Application ID
*/
public TrackerBuilder(Emitter emitter, String namespace, String appId) {
this.emitter = emitter;
this.namespace = namespace;
this.appId = appId;
}
/**
* @param subject Subject to be tracked
* @return itself
*/
public TrackerBuilder subject(Subject subject) {
this.subject = subject;
return this;
}
/**
* @param platform The device platform the tracker is running on
* @return itself
*/
public TrackerBuilder platform(DevicePlatform platform) {
this.platform = platform;
return this;
}
/**
* @param base64 Whether JSONs in the payload should be base-64 encoded
* @return itself
*/
public TrackerBuilder base64(Boolean base64) {
this.base64Encoded = base64;
return this;
}
/**
* Creates a new Tracker
*
* @return a new Tracker object
*/
public Tracker build() {
return new Tracker(this);
}
}
// --- Setters
/**
* @param emitter a new emitter
*/
public void setEmitter(Emitter emitter) {
this.emitter = emitter;
}
/**
* Sets a new Subject object which will get attached to
* each event payload.
*
* @param subject the new Subject
*/
public void setSubject(Subject subject) {
this.subject = subject;
}
// --- Getters
/**
* @return the emitter associated with the tracker
*/
public Emitter getEmitter() {
return this.emitter;
}
/**
* @return the Tracker Subject
*/
public Subject getSubject() {
return this.subject;
}
/**
* @return the tracker version that was set
*/
public String getTrackerVersion() {
return this.parameters.getTrackerVersion();
}
/**
* @return the trackers namespace
*/
public String getNamespace() {
return this.parameters.getNamespace();
}
/**
* @return the trackers set Application ID
*/
public String getAppId() {
return this.parameters.getAppId();
}
/**
* @return the base64 setting of the tracker
*/
public boolean getBase64Encoded() {
return this.parameters.getBase64Encoded();
}
/**
* @return the Tracker platform
*/
public DevicePlatform getPlatform() {
return this.parameters.getPlatform();
}
/**
* @return the wrapper containing the Tracker parameters
*/
public TrackerParameters getParameters() {
return this.parameters;
}
// --- Event Tracking Functions
/**
* Handles tracking the different types of events that
* the Tracker can encounter.
*
* @param event the event to track
*/
public void track(Event event) {
// Emit the event
this.emitter.emit(new TrackerEvent(event, this.parameters, this.subject));
}
}