Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hyperengage] Destination #1621

Merged
merged 13 commits into from
Oct 3, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { createTestIntegration } from '@segment/actions-core'
import Definition from '../index'
import nock from 'nock'

export const apiKey = 'testApiKey'
export const workspaceIdentifier = 'testApiIdentifier'

const testDestination = createTestIntegration(Definition)
beforeAll(() => {
nock.disableNetConnect()
})

afterAll(() => {
nock.enableNetConnect()
nock.cleanAll()
})

describe('Hyperengage', () => {
describe('testAuthentication', () => {
test('should validate workspaceIdentifier and apiKey', async () => {
nock('https://api.hyperengage.io/api/v1/verify_api_key')
.post(/.*/, {
api_key: apiKey,
workspace_identifier: workspaceIdentifier
})
.reply(200, { message: 'Mocked response' })
await expect(testDestination.testAuthentication({ apiKey, workspaceIdentifier })).resolves.not.toThrowError()
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { validateInput } from '../validateInput'

const fakeTrackData = {
event_id: 'test-message-cz380xxe9kn',
page_title: 'Title',
event_name: 'test',
event_type: 'track',
properties: {
required: 'false'
},
timestamp: '2023-09-11T08:06:11.192Z',
user_id: 'test',
account_id: 'testAccount'
}

const fakeIdentifyData = {
event_id: 'test-message-cz380xxe9kn',
page_title: 'Title',
event_name: 'test',
event_type: 'identify',
name: 'testUser',
email: 'testEmail',
traits: {
required: 'false'
},
timestamp: '2023-09-11T08:06:11.192Z',
user_id: 'test',
account_id: 'testAccount'
}

const fakeGroupData = {
event_id: 'test-message-cz380xxe9kn',
page_title: 'Title',
event_name: 'test',
event_type: 'group',
name: 'Test account',
plan: 'temporary',
industry: 'test industry',
website: 'test website',
traits: {
required: 'false'
},
timestamp: '2023-09-11T08:06:11.192Z',
user_id: 'test',
account_id: 'testAccount'
}

const settings = {
workspaceIdentifier: 'testWorkspaceId',
apiKey: 'testApiKey'
}

describe('validateInput', () => {
describe('test common payload', () => {
it('should return converted payload', () => {
const payload = validateInput(settings, fakeIdentifyData, 'user_identify')
expect(payload.api_key).toBe(settings.apiKey)
expect(payload.workspace_key).toBe(settings.workspaceIdentifier)
expect(payload.doc_encoding).toBe('UTF-8')
expect(payload.src).toBe('segment_api')
expect(payload.anonymous_id).toHaveLength(10)
})
})

describe('test identify payload', () => {
it('should return converted payload', async () => {
const payload = validateInput(settings, fakeIdentifyData, 'user_identify')
expect(payload.user_id).toEqual(fakeIdentifyData.user_id)
expect(payload.traits.email).toEqual(fakeIdentifyData.email)
expect(payload.traits.name).toEqual(fakeIdentifyData.name)
expect(payload.traits).toHaveProperty('required')
})
})

describe('test group payload', () => {
it('should return converted payload', async () => {
const payload = validateInput(settings, fakeGroupData, 'account_identify')
expect(payload.account_id).toEqual(fakeGroupData.account_id)
expect(payload.traits.plan_name).toEqual(fakeGroupData.plan)
expect(payload.traits.industry).toEqual(fakeGroupData.industry)
expect(payload.traits.website).toEqual(fakeGroupData.website)
expect(payload.traits).toHaveProperty('required')
})
})

describe('test track payload', () => {
it('should return converted payload', async () => {
let payload = validateInput(settings, fakeGroupData, 'account_identify')
expect(payload.event_type).toEqual('account_identify')
payload = validateInput(settings, fakeTrackData, 'track')
expect(payload.event_type).toEqual('test')
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { ActionDefinition } from '@segment/actions-core'
import { Settings } from '../encharge/generated-types'

export const commonFields: ActionDefinition<Settings>['fields'] = {
event_id: {
type: 'string',
required: false,
description: 'The ID of the event.',
label: 'Event ID',
default: { '@path': '$.messageId' }
},
doc_path: {
type: 'string',
required: false,
description: 'The path of the document.',
label: 'Document Path',
default: { '@path': '$.context.page.path' }
},
doc_search: {
type: 'string',
required: false,
description: 'The search query of the document.',
label: 'Document Search',
default: { '@path': '$.context.page.search' }
},
page_title: {
type: 'string',
required: false,
description: 'The title of the page where the event occurred.',
label: 'Page Title',
default: { '@path': '$.context.page.title' }
},
referer: {
type: 'string',
required: false,
description: 'The referrer of the page where the event occurred.',
label: 'Referrer',
default: { '@path': '$.context.page.referrer' }
},
url: {
type: 'string',
required: false,
description: 'The URL of the page where the event occurred.',
label: 'URL',
default: { '@path': '$.context.page.url' }
},
user_agent: {
type: 'string',
required: false,
description: 'The user agent of the browser.',
label: 'User Agent',
default: { '@path': '$.context.userAgent' }
},
user_language: {
type: 'string',
required: false,
description: 'The language of the browser.',
label: 'User Language',
default: { '@path': '$.context.locale' }
},
utc_time: {
type: 'string',
required: false,
description: 'The time of the event in UTC.',
label: 'UTC Time',
default: { '@path': '$.timestamp' }
},
utm: {
type: 'object',
required: false,
description: 'Information about the UTM parameters.',
label: 'UTM',
properties: {
source: {
label: 'Source',
description: 'The source of the campaign.',
type: 'string'
},
medium: {
label: 'Medium',
description: 'The medium of the campaign.',
type: 'string'
},
name: {
label: 'Name',
description: 'The name of the campaign.',
type: 'string'
},
term: {
label: 'Term',
description: 'The term of the campaign.',
type: 'string'
},
content: {
label: 'Content',
description: 'The content of the campaign.',
type: 'string'
}
},
default: {
source: { '@path': '$.context.campaign.source' },
medium: { '@path': '$.context.campaign.medium' },
name: { '@path': '$.context.campaign.name' },
term: { '@path': '$.context.campaign.term' },
content: { '@path': '$.context.campaign.content' }
}
},
screen: {
type: 'object',
required: false,
description: 'Information about the screen.',
label: 'Screen',
properties: {
height: {
label: 'Height',
description: 'The height of the screen.',
type: 'integer'
},
width: {
label: 'Width',
description: 'The width of the screen.',
type: 'integer'
},
density: {
label: 'Density',
description: 'The density of the screen.',
type: 'number'
}
},
default: {
height: { '@path': '$.context.screen.height' },
width: { '@path': '$.context.screen.width' },
density: { '@path': '$.context.screen.density' }
}
},
timezone: {
type: 'string',
required: false,
description: 'The timezone of the browser.',
label: 'Timezone',
default: {
'@if': {
exists: { '@path': '$.properties.timezone' },
then: { '@path': '$.properties.timezone' },
else: { '@path': '$.traits.timezone' }
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
default: {
'@if': {
exists: { '@path': '$.properties.timezone' },
then: { '@path': '$.properties.timezone' },
else: { '@path': '$.traits.timezone' }
}
}
default: { '@path': '$.context.timezone' }

},
source_ip: {
type: 'string',
required: false,
description: 'The IP address of the user.',
label: 'IP Address',
default: { '@path': '$.context.ip' }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import nock from 'nock'
import { createTestEvent, createTestIntegration } from '@segment/actions-core'
import Destination from '../../index'

const testDestination = createTestIntegration(Destination)

beforeEach(() => nock.cleanAll())

const heGroupMapping = {
account_id: {
'@path': '$.groupId'
},
name: {
'@if': {
exists: { '@path': '$.traits.name' },
then: { '@path': '$.traits.name' },
else: { '@path': '$.properties.name' }
}
},
created_at: {
'@path': '$.traits.created_at'
},
traits: {
'@path': '$.traits'
},
plan: {
'@path': '$.traits.plan'
},
industry: {
'@path': '$.traits.industry'
},
website: {
'@path': '$.traits.website'
}
}

describe('Hyperengage.group', () => {
test('Should throw an error if `account_id or` `name` is not defined', async () => {
const event = createTestEvent({
type: 'group',
traits: {
email: '[email protected]'
},
groupId: '[email protected]'
})

await expect(
testDestination.testAction('group', {
event,
mapping: heGroupMapping
})
).rejects.toThrowError()
})

test('Should throw an error if workspaceIdentifier or apiKey is not defined', async () => {
const event = createTestEvent({
type: 'group',
traits: {
name: 'test'
},
groupId: '123456'
})

await expect(
testDestination.testAction('group', {
event,
mapping: heGroupMapping,
settings: {
workspaceIdentifier: '',
apiKey: ''
}
})
).rejects.toThrowError()
})

test('Should send an group event to Hyperengage', async () => {
// Mock: Segment group Call
nock('https://events.hyperengage.io').post('/api/v1/s2s/event?token=apiKey').reply(200, { success: true })

const event = createTestEvent({
type: 'group',
traits: {
name: 'test'
},
groupId: '123456'
})

const responses = await testDestination.testAction('group', {
event,
mapping: heGroupMapping,
settings: {
workspaceIdentifier: 'identifier',
apiKey: 'apiKey'
}
})

expect(responses.length).toBe(1)
expect(responses[0].status).toEqual(200)
})
})
Loading