@streamscloud/streams-analytics-collector 1.0.0 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,114 @@
1
+ import type { Client } from '@urql/core';
2
+ /**
3
+ * AppEventsTracker is a utility class for tracking various user events in StreamsCloud applications
4
+ * such as post views, community message views, stream interactions, etc.
5
+ */
6
+ export declare class AppEventsTracker {
7
+ private static reported;
8
+ private static gqlEndpoint;
9
+ private static client;
10
+ private static organizationId;
11
+ private static profileId;
12
+ private static useClient;
13
+ /**
14
+ * Set the GraphQL client for making API calls (for authorized calls)
15
+ * @param client - The URQL client instance
16
+ */
17
+ static setClient(client: Client): void;
18
+ /**
19
+ * Set the GraphQL endpoint for making API calls (for non-authorized calls)
20
+ * @param endpoint - The GraphQL endpoint URL
21
+ */
22
+ static setEndpoint(endpoint: string): void;
23
+ /**
24
+ * Set organization ID for non-authorized calls
25
+ * @param organizationId - The organization ID
26
+ */
27
+ static setOrganizationId(organizationId: string): void;
28
+ /**
29
+ * Set profile ID for non-authorized calls
30
+ * @param profileId - The profile ID
31
+ */
32
+ static setProfileId(profileId: string): void;
33
+ /**
34
+ * Track when a post is opened
35
+ * @param postId - The ID of the post
36
+ * @param ownerId - The ID of the owner
37
+ */
38
+ static trackPostOpened(postId: string, ownerId: string): void;
39
+ /**
40
+ * Track when a post is being played
41
+ * @param postId - The ID of the post
42
+ * @param playedPercents - The percentage of the post that has been played
43
+ */
44
+ static trackPostPlaying(postId: string, playedPercents: number): void;
45
+ /**
46
+ * Track when a community message is opened
47
+ * @param messageId - The ID of the message
48
+ * @param status - The status of the message
49
+ */
50
+ static trackCommunityMessageOpened(messageId: string, status: string): void;
51
+ /**
52
+ * Track when a stream tile is shown to the user
53
+ * @param streamId - The ID of the stream
54
+ */
55
+ static trackStreamTileImpression(streamId: string): void;
56
+ /**
57
+ * Track when a stream tile is clicked
58
+ * @param streamId - The ID of the stream
59
+ */
60
+ static trackStreamTileClick(streamId: string): void;
61
+ /**
62
+ * Track when a stream is viewed
63
+ * @param streamId - The ID of the stream
64
+ */
65
+ static trackStreamView(streamId: string): void;
66
+ /**
67
+ * Track the amount of time a user engages with a stream
68
+ * @param streamId - The ID of the stream
69
+ * @param engagementTimeSeconds - The engagement time in seconds
70
+ */
71
+ static trackStreamEngagementTime(streamId: string, engagementTimeSeconds: number): void;
72
+ /**
73
+ * Track how deep a user scrolls in a stream
74
+ * @param streamId - The ID of the stream
75
+ * @param scrollDepth - The scroll depth (typically a percentage)
76
+ */
77
+ static trackStreamScrollDepth(streamId: string, scrollDepth: number): void;
78
+ /**
79
+ * Track when a stream page is viewed
80
+ * @param pageId - The ID of the page
81
+ * @param streamId - The ID of the stream
82
+ */
83
+ static trackStreamPageView(pageId: string, streamId: string): void;
84
+ /**
85
+ * Track when a product in a stream is clicked
86
+ * @param productId - The ID of the product
87
+ * @param streamId - The ID of the stream
88
+ */
89
+ static trackStreamProductClicked(productId: string, streamId: string): void;
90
+ /**
91
+ * Track when a post is closed
92
+ * @param postId - The ID of the post
93
+ */
94
+ static trackClosed(postId: string): void;
95
+ /**
96
+ * Report an app event to the API
97
+ * @private
98
+ * @param targetId - The ID of the target object
99
+ * @param eventType - The type of event
100
+ * @param ownerId - The ID of the owner (optional)
101
+ * @param value - Additional value for the event (optional)
102
+ */
103
+ private static reportAppEvent;
104
+ /**
105
+ * Save an app event to the API
106
+ * @private
107
+ * @param targetId - The ID of the target object
108
+ * @param eventType - The type of event
109
+ * @param ownerId - The ID of the owner (optional)
110
+ * @param value - Additional value for the event (optional)
111
+ */
112
+ private static saveAppEvent;
113
+ private static queryGql;
114
+ }
@@ -0,0 +1,223 @@
1
+ import { AppEventType, CommunityMessageStatus } from './types.js';
2
+ import AnalyticsQuery from '../analytics.graphql.js';
3
+
4
+ /**
5
+ * AppEventsTracker is a utility class for tracking various user events in StreamsCloud applications
6
+ * such as post views, community message views, stream interactions, etc.
7
+ */
8
+ class AppEventsTracker {
9
+ static reported = [];
10
+ static gqlEndpoint = '';
11
+ static client;
12
+ static organizationId;
13
+ static profileId;
14
+ static useClient = false;
15
+ /**
16
+ * Set the GraphQL client for making API calls (for authorized calls)
17
+ * @param client - The URQL client instance
18
+ */
19
+ static setClient(client) {
20
+ this.client = client;
21
+ this.useClient = true;
22
+ }
23
+ /**
24
+ * Set the GraphQL endpoint for making API calls (for non-authorized calls)
25
+ * @param endpoint - The GraphQL endpoint URL
26
+ */
27
+ static setEndpoint(endpoint) {
28
+ this.gqlEndpoint = endpoint;
29
+ this.useClient = false;
30
+ }
31
+ /**
32
+ * Set organization ID for non-authorized calls
33
+ * @param organizationId - The organization ID
34
+ */
35
+ static setOrganizationId(organizationId) {
36
+ this.organizationId = organizationId;
37
+ }
38
+ /**
39
+ * Set profile ID for non-authorized calls
40
+ * @param profileId - The profile ID
41
+ */
42
+ static setProfileId(profileId) {
43
+ this.profileId = profileId;
44
+ }
45
+ /**
46
+ * Track when a post is opened
47
+ * @param postId - The ID of the post
48
+ * @param ownerId - The ID of the owner
49
+ */
50
+ static trackPostOpened(postId, ownerId) {
51
+ this.reportAppEvent(postId, AppEventType.PostView, ownerId);
52
+ }
53
+ /**
54
+ * Track when a post is being played
55
+ * @param postId - The ID of the post
56
+ * @param playedPercents - The percentage of the post that has been played
57
+ */
58
+ static trackPostPlaying(postId, playedPercents) {
59
+ if (playedPercents > 0.2 && this.reported.indexOf(postId) === -1) {
60
+ this.reportAppEvent(postId, AppEventType.PostView);
61
+ }
62
+ }
63
+ /**
64
+ * Track when a community message is opened
65
+ * @param messageId - The ID of the message
66
+ * @param status - The status of the message
67
+ */
68
+ static trackCommunityMessageOpened(messageId, status) {
69
+ if (status === CommunityMessageStatus.Sent) {
70
+ this.reportAppEvent(messageId, AppEventType.CommunityMessageView);
71
+ }
72
+ }
73
+ /**
74
+ * Track when a stream tile is shown to the user
75
+ * @param streamId - The ID of the stream
76
+ */
77
+ static trackStreamTileImpression(streamId) {
78
+ this.reportAppEvent(streamId, AppEventType.StreamTileImpression);
79
+ }
80
+ /**
81
+ * Track when a stream tile is clicked
82
+ * @param streamId - The ID of the stream
83
+ */
84
+ static trackStreamTileClick(streamId) {
85
+ this.reportAppEvent(streamId, AppEventType.StreamTileClick);
86
+ }
87
+ /**
88
+ * Track when a stream is viewed
89
+ * @param streamId - The ID of the stream
90
+ */
91
+ static trackStreamView(streamId) {
92
+ this.reportAppEvent(streamId, AppEventType.StreamView);
93
+ }
94
+ /**
95
+ * Track the amount of time a user engages with a stream
96
+ * @param streamId - The ID of the stream
97
+ * @param engagementTimeSeconds - The engagement time in seconds
98
+ */
99
+ static trackStreamEngagementTime(streamId, engagementTimeSeconds) {
100
+ this.reportAppEvent(streamId, AppEventType.StreamEngagementTime, undefined, engagementTimeSeconds);
101
+ }
102
+ /**
103
+ * Track how deep a user scrolls in a stream
104
+ * @param streamId - The ID of the stream
105
+ * @param scrollDepth - The scroll depth (typically a percentage)
106
+ */
107
+ static trackStreamScrollDepth(streamId, scrollDepth) {
108
+ this.reportAppEvent(streamId, AppEventType.StreamScrollDepth, undefined, scrollDepth);
109
+ }
110
+ /**
111
+ * Track when a stream page is viewed
112
+ * @param pageId - The ID of the page
113
+ * @param streamId - The ID of the stream
114
+ */
115
+ static trackStreamPageView(pageId, streamId) {
116
+ this.reportAppEvent(pageId, AppEventType.StreamPageView, streamId);
117
+ }
118
+ /**
119
+ * Track when a product in a stream is clicked
120
+ * @param productId - The ID of the product
121
+ * @param streamId - The ID of the stream
122
+ */
123
+ static trackStreamProductClicked(productId, streamId) {
124
+ this.reportAppEvent(productId, AppEventType.StreamProductClick, streamId);
125
+ }
126
+ /**
127
+ * Track when a post is closed
128
+ * @param postId - The ID of the post
129
+ */
130
+ static trackClosed(postId) {
131
+ const postIndex = this.reported.indexOf(postId);
132
+ if (postIndex >= 0) {
133
+ this.reported.splice(postIndex, 1);
134
+ }
135
+ }
136
+ /**
137
+ * Report an app event to the API
138
+ * @private
139
+ * @param targetId - The ID of the target object
140
+ * @param eventType - The type of event
141
+ * @param ownerId - The ID of the owner (optional)
142
+ * @param value - Additional value for the event (optional)
143
+ */
144
+ static async reportAppEvent(targetId, eventType, ownerId, value) {
145
+ await this.saveAppEvent(targetId, eventType, ownerId, value);
146
+ }
147
+ /**
148
+ * Save an app event to the API
149
+ * @private
150
+ * @param targetId - The ID of the target object
151
+ * @param eventType - The type of event
152
+ * @param ownerId - The ID of the owner (optional)
153
+ * @param value - Additional value for the event (optional)
154
+ */
155
+ static async saveAppEvent(targetId, eventType, ownerId, value) {
156
+ // Create the input payload with the base properties
157
+ const input = {
158
+ eventType,
159
+ targetId,
160
+ ownerId: ownerId || undefined,
161
+ value: value || undefined
162
+ };
163
+ // Add organization and profile IDs for non-authorized calls if they exist
164
+ if (!this.useClient) {
165
+ if (this.organizationId) {
166
+ input.organizationId = this.organizationId;
167
+ }
168
+ if (this.profileId) {
169
+ input.profileId = this.profileId;
170
+ }
171
+ }
172
+ // Choose between client and direct endpoint modes
173
+ if (this.useClient) {
174
+ if (!this.client) {
175
+ console.warn('An attempt to save app event without initializing GraphQL client detected');
176
+ return;
177
+ }
178
+ // const mutation = `
179
+ // mutation TrackAppEvent($input: TrackAppEventInput!) {
180
+ // trackAppEvent(input: $input) {
181
+ // void
182
+ // }
183
+ // }
184
+ // `;
185
+ await this.client
186
+ .mutation(AnalyticsQuery, { input })
187
+ .toPromise();
188
+ }
189
+ else {
190
+ if (!this.gqlEndpoint) {
191
+ console.warn('An attempt to save app event without initializing GraphQL endpoint detected');
192
+ return;
193
+ }
194
+ // const query = `
195
+ // mutation TrackAppEvent($input: TrackAppEventInput!) {
196
+ // trackAppEvent(input: $input) {
197
+ // void
198
+ // }
199
+ // }
200
+ // `;
201
+ await this.queryGql(AnalyticsQuery, { input });
202
+ }
203
+ }
204
+ static queryGql = async (query, variables) => {
205
+ const response = await fetch(this.gqlEndpoint, {
206
+ method: 'POST',
207
+ headers: {
208
+ 'Content-Type': 'application/json'
209
+ },
210
+ body: JSON.stringify({
211
+ query,
212
+ variables
213
+ })
214
+ });
215
+ const gql = await response.json();
216
+ if (!gql.data) {
217
+ throw new Error(JSON.stringify(gql.errors));
218
+ }
219
+ return gql.data;
220
+ };
221
+ }
222
+
223
+ export { AppEventsTracker };
@@ -0,0 +1,2 @@
1
+ export { AppEventsTracker } from './app-events-tracker';
2
+ export { AppEventType, CommunityMessageStatus, type TrackAppEventInput } from './types';
@@ -0,0 +1,2 @@
1
+ export { AppEventsTracker } from './app-events-tracker.js';
2
+ export { AppEventType, CommunityMessageStatus } from './types.js';
@@ -0,0 +1,25 @@
1
+ export declare enum AppEventType {
2
+ PostView = "POST_VIEW",
3
+ CommunityMessageView = "COMMUNITY_MESSAGE_VIEW",
4
+ StreamTileImpression = "STREAM_TILE_IMPRESSION",
5
+ StreamTileClick = "STREAM_TILE_CLICK",
6
+ StreamView = "STREAM_VIEW",
7
+ StreamEngagementTime = "STREAM_ENGAGEMENT_TIME",
8
+ StreamScrollDepth = "STREAM_SCROLL_DEPTH",
9
+ StreamPageView = "STREAM_PAGE_VIEW",
10
+ StreamProductClick = "STREAM_PRODUCT_CLICK"
11
+ }
12
+ export declare enum CommunityMessageStatus {
13
+ Sent = "SENT",
14
+ Delivered = "DELIVERED",
15
+ Read = "READ",
16
+ Failed = "FAILED"
17
+ }
18
+ export type TrackAppEventInput = {
19
+ eventType: AppEventType;
20
+ targetId: string;
21
+ ownerId?: string;
22
+ value?: number;
23
+ organizationId?: string;
24
+ profileId?: string;
25
+ };
@@ -0,0 +1,21 @@
1
+ var AppEventType;
2
+ (function (AppEventType) {
3
+ AppEventType["PostView"] = "POST_VIEW";
4
+ AppEventType["CommunityMessageView"] = "COMMUNITY_MESSAGE_VIEW";
5
+ AppEventType["StreamTileImpression"] = "STREAM_TILE_IMPRESSION";
6
+ AppEventType["StreamTileClick"] = "STREAM_TILE_CLICK";
7
+ AppEventType["StreamView"] = "STREAM_VIEW";
8
+ AppEventType["StreamEngagementTime"] = "STREAM_ENGAGEMENT_TIME";
9
+ AppEventType["StreamScrollDepth"] = "STREAM_SCROLL_DEPTH";
10
+ AppEventType["StreamPageView"] = "STREAM_PAGE_VIEW";
11
+ AppEventType["StreamProductClick"] = "STREAM_PRODUCT_CLICK";
12
+ })(AppEventType || (AppEventType = {}));
13
+ var CommunityMessageStatus;
14
+ (function (CommunityMessageStatus) {
15
+ CommunityMessageStatus["Sent"] = "SENT";
16
+ CommunityMessageStatus["Delivered"] = "DELIVERED";
17
+ CommunityMessageStatus["Read"] = "READ";
18
+ CommunityMessageStatus["Failed"] = "FAILED";
19
+ })(CommunityMessageStatus || (CommunityMessageStatus = {}));
20
+
21
+ export { AppEventType, CommunityMessageStatus };
@@ -0,0 +1,3 @@
1
+ var AnalyticsQuery = "mutation TrackAppEvent($input: TrackAppEventInput!) {\n trackAppEvent(input: $input) {\n void\n }\n}\n";
2
+
3
+ export { AnalyticsQuery as default };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,2 @@
1
- export { StreamsContentApiClient } from './streams-content-api-client';
2
- export { type StreamsApiClientModel } from './streams-api-client-model';
3
1
  export * as Types from './types';
4
- export { SiteAdCampaignsOrderBy, SiteContentListsOrderBy, SitePostsOrderBy } from './types';
2
+ export { AppEventsTracker, AppEventType, CommunityMessageStatus } from './analytics';
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { StreamsContentApiClient } from './streams-content-api-client.js';
2
1
  import * as types from './types.js';
3
2
  export { types as Types };
4
- export { SiteAdCampaignsOrderBy, SiteContentListsOrderBy, SitePostsOrderBy } from './types.js';
3
+ export { AppEventsTracker } from './analytics/app-events-tracker.js';
4
+ export { AppEventType, CommunityMessageStatus } from './analytics/types.js';