Skip to content

Commit

Permalink
Merge branch 'develop' into fix.ga4
Browse files Browse the repository at this point in the history
  • Loading branch information
mihir-4116 authored Feb 14, 2024
2 parents 00b458f + f7ec0a1 commit 65ff64f
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/v0/destinations/am/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,16 +614,16 @@ const processSingleMessage = (message, destination) => {
case EventType.PAGE:
if (useUserDefinedPageEventName) {
const getMessagePath = userProvidedPageEventString
.substring(
?.substring(
userProvidedPageEventString.indexOf('{') + 2,
userProvidedPageEventString.indexOf('}'),
)
.trim();
evType =
userProvidedPageEventString.trim() === ''
userProvidedPageEventString?.trim() === ''
? name
: userProvidedPageEventString
.trim()
?.trim()
.replaceAll(/{{([^{}]+)}}/g, get(message, getMessagePath));
} else {
evType = `Viewed ${name || get(message, CATEGORY_KEY) || ''} Page`;
Expand Down Expand Up @@ -701,6 +701,7 @@ const processSingleMessage = (message, destination) => {
logger.debug('could not determine type');
throw new InstrumentationError('message type not supported');
}
AMUtils.validateEventType(evType);
return responseBuilderSimple(
groupInfo,
payloadObjectName,
Expand Down
33 changes: 32 additions & 1 deletion src/v0/destinations/am/util.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getUnsetObj } = require('./utils');
const { getUnsetObj, validateEventType } = require('./utils');

describe('getUnsetObj', () => {
it("should return undefined when 'message.integrations.Amplitude.fieldsToUnset' is not array", () => {
Expand Down Expand Up @@ -64,3 +64,34 @@ describe('getUnsetObj', () => {
expect(result).toBeUndefined();
});
});


describe('validateEventType', () => {

it('should validate event type when it is valid with only page name given', () => {
expect(() => {
validateEventType('Home Page');
}).not.toThrow();
});

it('should throw an error when event type is null', () => {
expect(() => {
validateEventType(null);
}).toThrow('Event type is missing. Please send it under `event.type`. For page/screen events, send it under `event.name`');
});

it('should throw an error when event type is undefined', () => {
expect(() => {
validateEventType(undefined);
}).toThrow('Event type is missing. Please send it under `event.type`. For page/screen events, send it under `event.name`');
});

// Function receives an empty string as event type
it('should throw an error when event type is an empty string', () => {
expect(() => {
validateEventType('');
}).toThrow('Event type is missing. Please send it under `event.type`. For page/screen events, send it under `event.name`');
});

});

15 changes: 15 additions & 0 deletions src/v0/destinations/am/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// populate these dest keys
const get = require('get-value');
const uaParser = require('@amplitude/ua-parser-js');
const { InstrumentationError } = require('@rudderstack/integrations-lib');
const logger = require('../../../logger');
const { isDefinedAndNotNull } = require('../../util');

Expand Down Expand Up @@ -108,6 +109,19 @@ const getUnsetObj = (message) => {

return unsetObject;
};

/**
* Check for evType as in some cases, like when the page name is absent,
* either the template depends only on the event.name or there is no template provided by user
* @param {*} evType
*/
const validateEventType = (evType) => {
if (!isDefinedAndNotNull(evType) || (typeof evType === "string" && evType.length ===0)) {
throw new InstrumentationError(
'Event type is missing. Please send it under `event.type`. For page/screen events, send it under `event.name`',
);
}
};
module.exports = {
getOSName,
getOSVersion,
Expand All @@ -117,4 +131,5 @@ module.exports = {
getBrand,
getEventId,
getUnsetObj,
validateEventType
};
2 changes: 1 addition & 1 deletion src/v0/util/facebookUtils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ const getContentType = (message, defaultValue, categoryToContent, destinationNam
return integrationsObj.contentType;
}

let { category } = properties;
let { category } = properties || {};
if (!category) {
const { products } = properties;
if (products && products.length > 0 && Array.isArray(products) && isObject(products[0])) {
Expand Down
53 changes: 53 additions & 0 deletions test/integrations/destinations/am/processor/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11327,4 +11327,57 @@ export const data = [
},
},
},
{
name: 'am',
description:
'Test 78 -> Page call invalid event type as page name and template is not provided',
feature: 'processor',
module: 'destination',
version: 'v0',
input: {
request: {
body: [
{
message: {
request_ip: '1.1.1.1',
type: 'page',
userId: '12345',
properties: {},
integrations: {
All: true,
},
sentAt: '2019-10-14T11:15:53.296Z',
},
destination: {
Config: {
apiKey: 'abcde',
useUserDefinedPageEventName: true,
userProvidedPageEventString: '',
},
},
},
],
},
},
output: {
response: {
status: 200,
body: [
{
statusCode: 400,
error:
'Event type is missing. Please send it under `event.type`. For page/screen events, send it under `event.name`',
statTags: {
errorCategory: 'dataValidation',
errorType: 'instrumentation',
destType: 'AM',
module: 'destination',
implementation: 'native',
feature: 'processor',
},
},
],
},
},
},
];

0 comments on commit 65ff64f

Please sign in to comment.