@streamscloud/streams-analytics-collector 1.0.0 → 1.0.1

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,94 @@
1
+ /**
2
+ * AppEventsTracker is a utility class for tracking various user events in StreamsCloud applications
3
+ * such as post views, community message views, stream interactions, etc.
4
+ */
5
+ export declare class AppEventsTracker {
6
+ private static reported;
7
+ private static gqlEndpoint;
8
+ /**
9
+ * Set the GraphQL endpoint for making API calls
10
+ * @param endpoint - The GraphQL endpoint URL
11
+ */
12
+ static setEndpoint(endpoint: string): void;
13
+ /**
14
+ * Track when a post is opened
15
+ * @param postId - The ID of the post
16
+ * @param ownerId - The ID of the owner
17
+ */
18
+ static trackPostOpened(postId: string, ownerId: string): void;
19
+ /**
20
+ * Track when a post is being played
21
+ * @param postId - The ID of the post
22
+ * @param playedPercents - The percentage of the post that has been played
23
+ */
24
+ static trackPostPlaying(postId: string, playedPercents: number): void;
25
+ /**
26
+ * Track when a community message is opened
27
+ * @param messageId - The ID of the message
28
+ * @param status - The status of the message
29
+ */
30
+ static trackCommunityMessageOpened(messageId: string, status: string): void;
31
+ /**
32
+ * Track when a stream tile is shown to the user
33
+ * @param streamId - The ID of the stream
34
+ */
35
+ static trackStreamTileImpression(streamId: string): void;
36
+ /**
37
+ * Track when a stream tile is clicked
38
+ * @param streamId - The ID of the stream
39
+ */
40
+ static trackStreamTileClick(streamId: string): void;
41
+ /**
42
+ * Track when a stream is viewed
43
+ * @param streamId - The ID of the stream
44
+ */
45
+ static trackStreamView(streamId: string): void;
46
+ /**
47
+ * Track the amount of time a user engages with a stream
48
+ * @param streamId - The ID of the stream
49
+ * @param engagementTimeSeconds - The engagement time in seconds
50
+ */
51
+ static trackStreamEngagementTime(streamId: string, engagementTimeSeconds: number): void;
52
+ /**
53
+ * Track how deep a user scrolls in a stream
54
+ * @param streamId - The ID of the stream
55
+ * @param scrollDepth - The scroll depth (typically a percentage)
56
+ */
57
+ static trackStreamScrollDepth(streamId: string, scrollDepth: number): void;
58
+ /**
59
+ * Track when a stream page is viewed
60
+ * @param pageId - The ID of the page
61
+ * @param streamId - The ID of the stream
62
+ */
63
+ static trackStreamPageView(pageId: string, streamId: string): void;
64
+ /**
65
+ * Track when a product in a stream is clicked
66
+ * @param productId - The ID of the product
67
+ * @param streamId - The ID of the stream
68
+ */
69
+ static trackStreamProductClicked(productId: string, streamId: string): void;
70
+ /**
71
+ * Track when a post is closed
72
+ * @param postId - The ID of the post
73
+ */
74
+ static trackClosed(postId: string): void;
75
+ /**
76
+ * Report an app event to the API
77
+ * @private
78
+ * @param targetId - The ID of the target object
79
+ * @param eventType - The type of event
80
+ * @param ownerId - The ID of the owner (optional)
81
+ * @param value - Additional value for the event (optional)
82
+ */
83
+ private static reportAppEvent;
84
+ /**
85
+ * Save an app event to the API
86
+ * @private
87
+ * @param targetId - The ID of the target object
88
+ * @param eventType - The type of event
89
+ * @param ownerId - The ID of the owner (optional)
90
+ * @param value - Additional value for the event (optional)
91
+ */
92
+ private static saveAppEvent;
93
+ private static queryGql;
94
+ }
@@ -0,0 +1,167 @@
1
+ import { AppEventType, CommunityMessageStatus } from './types.js';
2
+
3
+ /**
4
+ * AppEventsTracker is a utility class for tracking various user events in StreamsCloud applications
5
+ * such as post views, community message views, stream interactions, etc.
6
+ */
7
+ class AppEventsTracker {
8
+ static reported = [];
9
+ static gqlEndpoint = '';
10
+ /**
11
+ * Set the GraphQL endpoint for making API calls
12
+ * @param endpoint - The GraphQL endpoint URL
13
+ */
14
+ static setEndpoint(endpoint) {
15
+ this.gqlEndpoint = endpoint;
16
+ }
17
+ /**
18
+ * Track when a post is opened
19
+ * @param postId - The ID of the post
20
+ * @param ownerId - The ID of the owner
21
+ */
22
+ static trackPostOpened(postId, ownerId) {
23
+ this.reportAppEvent(postId, AppEventType.PostView, ownerId);
24
+ }
25
+ /**
26
+ * Track when a post is being played
27
+ * @param postId - The ID of the post
28
+ * @param playedPercents - The percentage of the post that has been played
29
+ */
30
+ static trackPostPlaying(postId, playedPercents) {
31
+ if (playedPercents > 0.2 && this.reported.indexOf(postId) === -1) {
32
+ this.reportAppEvent(postId, AppEventType.PostView);
33
+ }
34
+ }
35
+ /**
36
+ * Track when a community message is opened
37
+ * @param messageId - The ID of the message
38
+ * @param status - The status of the message
39
+ */
40
+ static trackCommunityMessageOpened(messageId, status) {
41
+ if (status === CommunityMessageStatus.Sent) {
42
+ this.reportAppEvent(messageId, AppEventType.CommunityMessageView);
43
+ }
44
+ }
45
+ /**
46
+ * Track when a stream tile is shown to the user
47
+ * @param streamId - The ID of the stream
48
+ */
49
+ static trackStreamTileImpression(streamId) {
50
+ this.reportAppEvent(streamId, AppEventType.StreamTileImpression);
51
+ }
52
+ /**
53
+ * Track when a stream tile is clicked
54
+ * @param streamId - The ID of the stream
55
+ */
56
+ static trackStreamTileClick(streamId) {
57
+ this.reportAppEvent(streamId, AppEventType.StreamTileClick);
58
+ }
59
+ /**
60
+ * Track when a stream is viewed
61
+ * @param streamId - The ID of the stream
62
+ */
63
+ static trackStreamView(streamId) {
64
+ this.reportAppEvent(streamId, AppEventType.StreamView);
65
+ }
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, engagementTimeSeconds) {
72
+ this.reportAppEvent(streamId, AppEventType.StreamEngagementTime, undefined, engagementTimeSeconds);
73
+ }
74
+ /**
75
+ * Track how deep a user scrolls in a stream
76
+ * @param streamId - The ID of the stream
77
+ * @param scrollDepth - The scroll depth (typically a percentage)
78
+ */
79
+ static trackStreamScrollDepth(streamId, scrollDepth) {
80
+ this.reportAppEvent(streamId, AppEventType.StreamScrollDepth, undefined, scrollDepth);
81
+ }
82
+ /**
83
+ * Track when a stream page is viewed
84
+ * @param pageId - The ID of the page
85
+ * @param streamId - The ID of the stream
86
+ */
87
+ static trackStreamPageView(pageId, streamId) {
88
+ this.reportAppEvent(pageId, AppEventType.StreamPageView, streamId);
89
+ }
90
+ /**
91
+ * Track when a product in a stream is clicked
92
+ * @param productId - The ID of the product
93
+ * @param streamId - The ID of the stream
94
+ */
95
+ static trackStreamProductClicked(productId, streamId) {
96
+ this.reportAppEvent(productId, AppEventType.StreamProductClick, streamId);
97
+ }
98
+ /**
99
+ * Track when a post is closed
100
+ * @param postId - The ID of the post
101
+ */
102
+ static trackClosed(postId) {
103
+ const postIndex = this.reported.indexOf(postId);
104
+ if (postIndex >= 0) {
105
+ this.reported.splice(postIndex, 1);
106
+ }
107
+ }
108
+ /**
109
+ * Report an app event to the API
110
+ * @private
111
+ * @param targetId - The ID of the target object
112
+ * @param eventType - The type of event
113
+ * @param ownerId - The ID of the owner (optional)
114
+ * @param value - Additional value for the event (optional)
115
+ */
116
+ static async reportAppEvent(targetId, eventType, ownerId, value) {
117
+ await this.saveAppEvent(targetId, eventType, ownerId, value);
118
+ }
119
+ /**
120
+ * Save an app event to the API
121
+ * @private
122
+ * @param targetId - The ID of the target object
123
+ * @param eventType - The type of event
124
+ * @param ownerId - The ID of the owner (optional)
125
+ * @param value - Additional value for the event (optional)
126
+ */
127
+ static async saveAppEvent(targetId, eventType, ownerId, value) {
128
+ if (!this.gqlEndpoint) {
129
+ console.warn('An attempt to save app event without initializing GraphQL endpoint detected');
130
+ return;
131
+ }
132
+ const query = `
133
+ mutation TrackAppEvent($input: TrackAppEventInput!) {
134
+ trackAppEvent(input: $input) {
135
+ void
136
+ }
137
+ }
138
+ `;
139
+ await this.queryGql(query, {
140
+ input: {
141
+ eventType,
142
+ targetId,
143
+ ownerId,
144
+ value
145
+ }
146
+ });
147
+ }
148
+ static queryGql = async (query, variables) => {
149
+ const response = await fetch(this.gqlEndpoint, {
150
+ method: 'POST',
151
+ headers: {
152
+ 'Content-Type': 'application/json'
153
+ },
154
+ body: JSON.stringify({
155
+ query,
156
+ variables
157
+ })
158
+ });
159
+ const gql = await response.json();
160
+ if (!gql.data) {
161
+ throw new Error(JSON.stringify(gql.errors));
162
+ }
163
+ return gql.data;
164
+ };
165
+ }
166
+
167
+ export { AppEventsTracker };
@@ -0,0 +1,2 @@
1
+ export { AppEventsTracker } from './app-events-tracker';
2
+ export { AppEventType, CommunityMessageStatus, type TrackAppEventInput } from './types';
@@ -0,0 +1,23 @@
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
+ };
@@ -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 };
package/dist/index.d.ts CHANGED
@@ -2,3 +2,4 @@ export { StreamsContentApiClient } from './streams-content-api-client';
2
2
  export { type StreamsApiClientModel } from './streams-api-client-model';
3
3
  export * as Types from './types';
4
4
  export { SiteAdCampaignsOrderBy, SiteContentListsOrderBy, SitePostsOrderBy } from './types';
5
+ export { AppEventsTracker, AppEventType, CommunityMessageStatus } from './analytics';
package/dist/index.js CHANGED
@@ -2,3 +2,5 @@ export { StreamsContentApiClient } from './streams-content-api-client.js';
2
2
  import * as types from './types.js';
3
3
  export { types as Types };
4
4
  export { SiteAdCampaignsOrderBy, SiteContentListsOrderBy, SitePostsOrderBy } from './types.js';
5
+ export { AppEventsTracker } from './analytics/app-events-tracker.js';
6
+ export { AppEventType, CommunityMessageStatus } from './analytics/types.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@streamscloud/streams-analytics-collector",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -11,6 +11,10 @@
11
11
  "types": "./dist/index.d.ts",
12
12
  "svelte": "./dist/index.js"
13
13
  },
14
+ "./analytics": {
15
+ "import": "./dist/analytics/index.js",
16
+ "types": "./dist/analytics/index.d.ts"
17
+ },
14
18
  "./data-loaders": {
15
19
  "import": "./dist/data-loaders/index.js",
16
20
  "types": "./dist/data-loaders/index.d.ts"