Skip to content

Commit 972f3a0

Browse files
Merge pull request #48 from newrelic/fix/custom-event-attribute-enrinchment
fix: Enrich custom event with attributes
2 parents afecd44 + 65033b2 commit 972f3a0

File tree

7 files changed

+64
-10
lines changed

7 files changed

+64
-10
lines changed

NRExoPlayerTracker/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
minSdkVersion 16
99
targetSdkVersion 33
1010
versionCode 6
11-
versionName "4.0.0"
11+
versionName "4.0.1"
1212
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1313
consumerProguardFiles "consumer-rules.pro"
1414
}

NRIMATracker/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
minSdkVersion 16
99
targetSdkVersion 33
1010
versionCode 6
11-
versionName "4.0.0"
11+
versionName "4.0.1"
1212
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1313
consumerProguardFiles "consumer-rules.pro"
1414
}

NewRelicVideoCore/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
minSdkVersion 16
99
targetSdkVersion 33
1010
versionCode 9
11-
versionName "4.0.0"
11+
versionName "4.0.1"
1212
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1313
consumerProguardFiles "consumer-rules.pro"
1414
}

NewRelicVideoCore/src/main/java/com/newrelic/videoagent/core/NRVideo.java

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,59 @@ public static void recordEvent(String eventType, Map<String, Object> attributes)
115115
}
116116

117117
/**
118-
* Static convenience method for recording custom video events
118+
* Static convenience method for recording custom video events to all active trackers
119119
*
120-
* @param attributes A map of attributes for the event.
120+
* @param attributes A map of attributes for the event. Must contain an "actionName" key.
121121
*/
122122
public static void recordCustomEvent(Map<String, Object> attributes) {
123-
recordEvent("VideoCustomAction", attributes);
123+
recordCustomEvent(attributes, null);
124+
}
125+
126+
/**
127+
* Static convenience method for recording custom video events with tracker ID support
128+
*
129+
* @param attributes A map of attributes for the event. Must contain an "action" key.
130+
* @param trackerId The tracker ID to send the event to. If null, event is sent to all active trackers.
131+
*/
132+
public static void recordCustomEvent(Map<String, Object> attributes, Integer trackerId) {
133+
if (!isInitialized()) {
134+
NRLog.w("recordCustomEvent called before NRVideo is fully initialized - event dropped");
135+
return;
136+
}
137+
138+
if (attributes == null || attributes.isEmpty()) {
139+
NRLog.w("Attributes parameter is mandatory for custom events");
140+
return;
141+
}
142+
143+
// Extract and validate action from attributes
144+
Object actionObj = attributes.get("actionName");
145+
if (actionObj == null || actionObj.toString().isEmpty()) {
146+
NRLog.w("Action attribute is mandatory for custom events - must be included in attributes map with key 'actionName'");
147+
return;
148+
}
149+
String action = actionObj.toString();
150+
151+
if (trackerId != null) {
152+
NRTracker contentTracker = NewRelicVideoAgent.getInstance().getContentTracker(trackerId);
153+
if (contentTracker != null) {
154+
contentTracker.sendEvent(action, attributes);
155+
}
156+
} else {
157+
// Global event - send to all trackers
158+
NRVideo videoInstance = getInstance();
159+
if (videoInstance == null || videoInstance.trackerIds.isEmpty()) {
160+
return;
161+
}
162+
163+
// Send to all trackers
164+
for (Integer currentTrackerId : videoInstance.trackerIds.values()) {
165+
NRTracker contentTracker = NewRelicVideoAgent.getInstance().getContentTracker(currentTrackerId);
166+
if (contentTracker != null) {
167+
contentTracker.sendEvent(action, attributes);
168+
}
169+
}
170+
}
124171
}
125172

126173
/**

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ dependencies {
4949
...
5050
5151
// Add this to install the NewRelicVideoCore (required)
52-
implementation 'com.github.newrelic.video-agent-android:NewRelicVideoCore:v4.0.0'
52+
implementation 'com.github.newrelic.video-agent-android:NewRelicVideoCore:v4.0.1'
5353
5454
// Add this to install the ExoPlayer tracker
55-
implementation 'com.github.newrelic.video-agent-android:NRExoPlayerTracker:v4.0.0'
55+
implementation 'com.github.newrelic.video-agent-android:NRExoPlayerTracker:v4.0.1'
5656
5757
// Add this to install the Google IMA library tracker
58-
implementation 'com.github.newrelic.video-agent-android:NRIMATracker:v4.0.0'
58+
implementation 'com.github.newrelic.video-agent-android:NRIMATracker:v4.0.1'
5959
}
6060
```
6161

app/src/main/java/com/newrelic/nrvideoproject/MainActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ protected void onCreate(Bundle savedInstanceState) {
3939
public void onClick(View view) {
4040
Map<String, Object> attr = new HashMap<>();
4141
attr.put("kind", "counter");
42+
attr.put("actionName", "CLICK");
4243
attr.put("counter", counter++);
4344
NRVideo.recordCustomEvent(attr);
4445

app/src/main/java/com/newrelic/nrvideoproject/VideoPlayer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,15 @@ private void playVideo(String videoUrl) {
6262
customAttr.put("myAttrStr", "Hello");
6363
customAttr.put("myAttrInt", 101);
6464
customAttr.put("name", "nr-video-agent-android-01-24JUL-john-starc");
65-
NRVideoPlayerConfiguration playerConfiguration = new NRVideoPlayerConfiguration("test-player", player, true, customAttr);
65+
NRVideoPlayerConfiguration playerConfiguration = new NRVideoPlayerConfiguration("test-player", player, false, customAttr);
6666
trackerId = NRVideo.addPlayer(playerConfiguration);
6767

68+
Map<String, Object> attributes = new HashMap<>();
69+
attributes.put("actionName", "VIDEO_STARTED");
70+
attributes.put("videoUrl", videoUrl);
71+
attributes.put("playerType", "ExoPlayer");
72+
NRVideo.recordCustomEvent(attributes, trackerId);
73+
6874
PlayerView playerView = findViewById(R.id.player);
6975
playerView.setPlayer(player);
7076
// Set the playlist URIs

0 commit comments

Comments
 (0)