From cdc89d3c982706f86631d9a32713949e00743429 Mon Sep 17 00:00:00 2001 From: AASHISH MALIK Date: Mon, 13 Nov 2023 02:19:52 +0530 Subject: [PATCH 1/3] feat: use custom screen name amplitude --- src/v0/destinations/am/transform.js | 27 +++-- test/__tests__/data/am_input.json | 100 ++++++++++++++++++ test/__tests__/data/am_output.json | 156 ++++++++++++++++++++++++++++ 3 files changed, 276 insertions(+), 7 deletions(-) diff --git a/src/v0/destinations/am/transform.js b/src/v0/destinations/am/transform.js index d7101982a7..ccd1cf6ef0 100644 --- a/src/v0/destinations/am/transform.js +++ b/src/v0/destinations/am/transform.js @@ -593,7 +593,8 @@ const processSingleMessage = (message, destination) => { const { name, event, properties } = message; const messageType = message.type.toLowerCase(); const CATEGORY_KEY = 'properties.category'; - const { useUserDefinedPageEventName, userProvidedPageEventString } = destination.Config; + const { useUserDefinedPageEventName, userProvidedPageEventString, + useUserDefinedScreenEventName, userProvidedScreenEventString } = destination.Config; switch (messageType) { case EventType.IDENTIFY: payloadObjectName = 'events'; // identify same as events @@ -629,15 +630,27 @@ const processSingleMessage = (message, destination) => { category = ConfigCategory.PAGE; break; case EventType.SCREEN: - { - const { eventType, updatedProperties } = getScreenevTypeAndUpdatedProperties( + let { eventType, updatedProperties } = getScreenevTypeAndUpdatedProperties( message, CATEGORY_KEY, - ); - evType = eventType; - message.properties = updatedProperties; - category = ConfigCategory.SCREEN; + ); + if (useUserDefinedScreenEventName) { + const getMessagePath = userProvidedScreenEventString + .substring( + userProvidedScreenEventString.indexOf('{') + 2, + userProvidedScreenEventString.indexOf('}'), + ) + .trim(); + eventType = + userProvidedScreenEventString.trim() === '' + ? eventType + : userProvidedScreenEventString + .trim() + .replaceAll(/{{([^{}]+)}}/g, get(message, getMessagePath)); } + evType = eventType; + message.properties = updatedProperties; + category = ConfigCategory.SCREEN; break; case EventType.GROUP: evType = 'group'; diff --git a/test/__tests__/data/am_input.json b/test/__tests__/data/am_input.json index 3e5cde2cca..126d235787 100644 --- a/test/__tests__/data/am_input.json +++ b/test/__tests__/data/am_input.json @@ -4842,5 +4842,105 @@ "userProvidedPageEventString": "Viewed {{context.page.title}} event." } } + }, + { + "message": { + "type": "screen", + "userId": "identified user id", + "anonymousId": "anon-id-new", + "event": "Screen View", + "properties": { + "prop1": "5" + }, + "context": { + "ip": "14.5.67.21", + "library": { + "name": "http" + } + }, + "timestamp": "2020-02-02T00:23:09.544Z" + }, + "destination": { + "Config": { + "apiKey": "abcde", + "useUserDefinedScreenEventName": true, + "userProvidedScreenEventString": "My {{ event }} event." + } + } + }, + { + "message": { + "type": "screen", + "userId": "identified user id", + "anonymousId": "anon-id-new", + "event": "Screen View", + "properties": { + "prop1": "5" + }, + "context": { + "ip": "14.5.67.21", + "library": { + "name": "http" + } + }, + "timestamp": "2020-02-02T00:23:09.544Z" + }, + "destination": { + "Config": { + "apiKey": "abcde", + "useUserDefinedScreenEventName": false, + "userProvidedScreenEventString": "My {{ event }} event." + } + } + }, + { + "message": { + "type": "screen", + "userId": "identified user id", + "anonymousId": "anon-id-new", + "event": "Screen same as event", + "properties": { + "prop1": "5" + }, + "context": { + "ip": "14.5.67.21", + "library": { + "name": "http" + } + }, + "timestamp": "2020-02-02T00:23:09.544Z" + }, + "destination": { + "Config": { + "apiKey": "abcde", + "useUserDefinedScreenEventName": true, + "userProvidedScreenEventString": "{{ event }}" + } + } + }, + { + "message": { + "type": "screen", + "userId": "identified user id", + "anonymousId": "anon-id-new", + "name": "Screen", + "properties": { + "prop1": "5" + }, + "context": { + "ip": "14.5.67.21", + "library": { + "name": "http" + } + }, + "timestamp": "2020-02-02T00:23:09.544Z" + }, + "destination": { + "Config": { + "apiKey": "abcde", + "useUserDefinedScreenEventName": true, + "userProvidedScreenEventString": "" + } + } } ] \ No newline at end of file diff --git a/test/__tests__/data/am_output.json b/test/__tests__/data/am_output.json index 34471f4922..303f1ee090 100644 --- a/test/__tests__/data/am_output.json +++ b/test/__tests__/data/am_output.json @@ -4311,5 +4311,161 @@ }, "files": {}, "userId": "00000000000000000000000000" + }, + { + "version": "1", + "type": "REST", + "method": "POST", + "endpoint": "https://api2.amplitude.com/2/httpapi", + "headers": { + "Content-Type": "application/json" + }, + "params": {}, + "body": { + "JSON": { + "api_key": "abcde", + "events": [ + { + "device_id": "anon-id-new", + "library": "rudderstack", + "event_properties": { + "prop1": "5", + "name": "Screen View" + }, + "event_type": "My Screen View event.", + "time": 1580602989544, + "user_id": "identified user id", + "user_properties": {}, + "session_id": -1, + "ip": "14.5.67.21" + } + ], + "options": { + "min_id_length": 1 + } + }, + "XML": {}, + "JSON_ARRAY": {}, + "FORM": {} + }, + "files": {}, + "userId": "anon-id-new" + }, + { + "version": "1", + "type": "REST", + "method": "POST", + "endpoint": "https://api2.amplitude.com/2/httpapi", + "headers": { + "Content-Type": "application/json" + }, + "params": {}, + "body": { + "JSON": { + "api_key": "abcde", + "events": [ + { + "device_id": "anon-id-new", + "library": "rudderstack", + "event_properties": { + "prop1": "5", + "name": "Screen View" + }, + "event_type": "Viewed Screen View Screen", + "time": 1580602989544, + "user_id": "identified user id", + "user_properties": {}, + "session_id": -1, + "ip": "14.5.67.21" + } + ], + "options": { + "min_id_length": 1 + } + }, + "XML": {}, + "JSON_ARRAY": {}, + "FORM": {} + }, + "files": {}, + "userId": "anon-id-new" + }, + { + "version": "1", + "type": "REST", + "method": "POST", + "endpoint": "https://api2.amplitude.com/2/httpapi", + "headers": { + "Content-Type": "application/json" + }, + "params": {}, + "body": { + "JSON": { + "api_key": "abcde", + "events": [ + { + "device_id": "anon-id-new", + "library": "rudderstack", + "event_properties": { + "prop1": "5", + "name": "Screen same as event" + }, + "event_type": "Screen same as event", + "time": 1580602989544, + "user_id": "identified user id", + "user_properties": {}, + "session_id": -1, + "ip": "14.5.67.21" + } + ], + "options": { + "min_id_length": 1 + } + }, + "XML": {}, + "JSON_ARRAY": {}, + "FORM": {} + }, + "files": {}, + "userId": "anon-id-new" + }, + { + "version": "1", + "type": "REST", + "method": "POST", + "endpoint": "https://api2.amplitude.com/2/httpapi", + "headers": { + "Content-Type": "application/json" + }, + "params": {}, + "body": { + "JSON": { + "api_key": "abcde", + "events": [ + { + "device_id": "anon-id-new", + "library": "rudderstack", + "event_properties": { + "prop1": "5", + "name": "Screen" + }, + "event_type": "Viewed Screen Screen", + "time": 1580602989544, + "user_id": "identified user id", + "user_properties": {}, + "session_id": -1, + "ip": "14.5.67.21" + } + ], + "options": { + "min_id_length": 1 + } + }, + "XML": {}, + "JSON_ARRAY": {}, + "FORM": {} + }, + "files": {}, + "userId": "anon-id-new" } ] \ No newline at end of file From 210a1cc1c2413163d37e260d6fc562052ebdd197 Mon Sep 17 00:00:00 2001 From: AASHISH MALIK Date: Mon, 13 Nov 2023 02:44:17 +0530 Subject: [PATCH 2/3] feat: use custom screen name amplitude --- src/v0/destinations/am/transform.js | 69 +++++++++++++++-------------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/src/v0/destinations/am/transform.js b/src/v0/destinations/am/transform.js index ccd1cf6ef0..44b18cc495 100644 --- a/src/v0/destinations/am/transform.js +++ b/src/v0/destinations/am/transform.js @@ -604,22 +604,22 @@ const processSingleMessage = (message, destination) => { case EventType.PAGE: if (useUserDefinedPageEventName) { const getMessagePath = userProvidedPageEventString - .substring( - userProvidedPageEventString.indexOf('{') + 2, - userProvidedPageEventString.indexOf('}'), - ) - .trim(); + .substring( + userProvidedPageEventString.indexOf('{') + 2, + userProvidedPageEventString.indexOf('}'), + ) + .trim(); evType = - userProvidedPageEventString.trim() === '' - ? name - : userProvidedPageEventString - .trim() - .replaceAll(/{{([^{}]+)}}/g, get(message, getMessagePath)); + userProvidedPageEventString.trim() === '' + ? name + : userProvidedPageEventString + .trim() + .replaceAll(/{{([^{}]+)}}/g, get(message, getMessagePath)); } else { const updatedName = getUpdatedPageNameWithoutUserDefinedPageEventName( - name, - message, - CATEGORY_KEY, + name, + message, + CATEGORY_KEY, ); evType = `Viewed ${updatedName || ''}Page`; } @@ -630,27 +630,30 @@ const processSingleMessage = (message, destination) => { category = ConfigCategory.PAGE; break; case EventType.SCREEN: - let { eventType, updatedProperties } = getScreenevTypeAndUpdatedProperties( - message, - CATEGORY_KEY, - ); - if (useUserDefinedScreenEventName) { - const getMessagePath = userProvidedScreenEventString - .substring( - userProvidedScreenEventString.indexOf('{') + 2, - userProvidedScreenEventString.indexOf('}'), - ) - .trim(); - eventType = - userProvidedScreenEventString.trim() === '' - ? eventType - : userProvidedScreenEventString - .trim() - .replaceAll(/{{([^{}]+)}}/g, get(message, getMessagePath)); + { + const { eventType, updatedProperties } = getScreenevTypeAndUpdatedProperties( + message, + CATEGORY_KEY, + ); + let customScreenEv = ''; + if (useUserDefinedScreenEventName) { + const getMessagePath = userProvidedScreenEventString + .substring( + userProvidedScreenEventString.indexOf('{') + 2, + userProvidedScreenEventString.indexOf('}'), + ) + .trim(); + customScreenEv = + userProvidedScreenEventString.trim() === '' + ? eventType + : userProvidedScreenEventString + .trim() + .replaceAll(/{{([^{}]+)}}/g, get(message, getMessagePath)); + } + evType =useUserDefinedScreenEventName ? customScreenEv : eventType; + message.properties = updatedProperties; + category = ConfigCategory.SCREEN; } - evType = eventType; - message.properties = updatedProperties; - category = ConfigCategory.SCREEN; break; case EventType.GROUP: evType = 'group'; From 4ff110f1c063d52d3eb380653b7c231f44f526ad Mon Sep 17 00:00:00 2001 From: AASHISH MALIK Date: Mon, 13 Nov 2023 13:06:35 +0530 Subject: [PATCH 3/3] fix: use name if custom screen name is empty --- src/v0/destinations/am/transform.js | 2 +- test/__tests__/data/am_output.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/v0/destinations/am/transform.js b/src/v0/destinations/am/transform.js index 2c5efdc437..bed5d45b28 100644 --- a/src/v0/destinations/am/transform.js +++ b/src/v0/destinations/am/transform.js @@ -638,7 +638,7 @@ const processSingleMessage = (message, destination) => { .trim(); customScreenEv = userProvidedScreenEventString.trim() === '' - ? eventType + ? name : userProvidedScreenEventString .trim() .replaceAll(/{{([^{}]+)}}/g, get(message, getMessagePath)); diff --git a/test/__tests__/data/am_output.json b/test/__tests__/data/am_output.json index a9fa3206d6..5a9c674113 100644 --- a/test/__tests__/data/am_output.json +++ b/test/__tests__/data/am_output.json @@ -4449,7 +4449,7 @@ "prop1": "5", "name": "Screen" }, - "event_type": "Viewed Screen Screen", + "event_type": "Screen", "time": 1580602989544, "user_id": "identified user id", "user_properties": {},