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

Launchpad Segment Integration #1010

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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,27 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing snapshot for Launchpad's groupIdentifyUser destination action: all fields 1`] = `
Object {
"$set": Object {
"group_id": "$WBBWnR",
"group_testType": "$WBBWnR",
},
"anonymoud_id": "$WBBWnR",
"api_key": "$WBBWnR",
"distinct_id": "$WBBWnR",
"event": "$identify",
"type": "screen",
"user_id": "$WBBWnR",
}
`;

exports[`Testing snapshot for Launchpad's groupIdentifyUser destination action: required fields 1`] = `
Object {
"$set": Object {
"group_id": "$WBBWnR",
},
"api_key": "$WBBWnR",
"event": "$identify",
"type": "screen",
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import nock from 'nock'
import { createTestIntegration } from '@segment/actions-core'
import Destination from '../../index'
import { SegmentEvent } from '@segment/actions-core'

const launchpadAPISecret = 'lp-api-key'
const timestamp = '2023-01-28T15:21:15.449Z'

const testDestination = createTestIntegration(Destination)

const expectedTraits = {
group_name: 'Launchpad',
group_industry: 'Technology',
group_employees: 3,
group_plan: '1',
'group_ARR(m)': 1503
}

const testGroupIdentify: SegmentEvent = {
messageId: 'test-message-t73406chv4',
timestamp: timestamp,
type: 'group',
groupId: '12381923812',
userId: '[email protected]',
traits: {
name: 'Launchpad',
industry: 'Technology',
employees: 3,
plan: '1',
'ARR(m)': 1503
}
}

describe('Launchpad.groupIdentifyUser', () => {
it('should convert the type and event name', async () => {
nock('https://data.launchpad.pm').post('/capture').reply(200, {})

const responses = await testDestination.testAction('groupIdentifyUser', {
event: testGroupIdentify,
useDefaultMappings: true,
settings: {
apiSecret: launchpadAPISecret,
sourceName: 'example segment source name'
}
})
expect(responses.length).toBe(1)
expect(responses[0].status).toBe(200)
expect(responses[0].data).toMatchObject({})
expect(responses[0].options.json).toMatchObject({
event: '$identify',
type: 'screen',
$set: expect.objectContaining(expectedTraits)
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { createTestEvent, createTestIntegration } from '@segment/actions-core'
import { generateTestData } from '../../../../lib/test-data'
import destination from '../../index'
import nock from 'nock'

const testDestination = createTestIntegration(destination)
const actionSlug = 'groupIdentifyUser'
const destinationSlug = 'Launchpad'
const seedName = `${destinationSlug}#${actionSlug}`

describe(`Testing snapshot for ${destinationSlug}'s ${actionSlug} destination action:`, () => {
it('required fields', async () => {
const action = destination.actions[actionSlug]
const [eventData, settingsData] = generateTestData(seedName, destination, action, true)

nock(/.*/).persist().get(/.*/).reply(200)
nock(/.*/).persist().post(/.*/).reply(200)
nock(/.*/).persist().put(/.*/).reply(200)

const event = createTestEvent({
properties: eventData
})
event.userId = 'user1234'

const responses = await testDestination.testAction(actionSlug, {
event: event,
mapping: event.properties,
settings: settingsData,
auth: undefined
})

const request = responses[0].request
const rawBody = await request.text()

try {
const json = JSON.parse(rawBody)
expect(json).toMatchSnapshot()
return
} catch (err) {
expect(rawBody).toMatchSnapshot()
}

expect(request.headers).toMatchSnapshot()
})

it('all fields', async () => {
const action = destination.actions[actionSlug]
const [eventData, settingsData] = generateTestData(seedName, destination, action, false)

nock(/.*/).persist().get(/.*/).reply(200)
nock(/.*/).persist().post(/.*/).reply(200)
nock(/.*/).persist().put(/.*/).reply(200)

const event = createTestEvent({
properties: eventData
})

const responses = await testDestination.testAction(actionSlug, {
event: event,
mapping: event.properties,
settings: settingsData,
auth: undefined
})

const request = responses[0].request
const rawBody = await request.text()

try {
const json = JSON.parse(rawBody)
expect(json).toMatchSnapshot()
return
} catch (err) {
expect(rawBody).toMatchSnapshot()
}
})
})

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,89 @@
import { ActionDefinition } from '@segment/actions-core'
import type { Settings } from '../generated-types'
import { getApiServerUrl } from '../utils'
import type { Payload } from './generated-types'

const groupIdentifyUser: ActionDefinition<Settings, Payload> = {
title: 'Group Identify User',
description:
'Updates or adds properties to a group profile. The profile is created if it does not exist. [Learn more about Group Analytics.](https://help.Launchpad.pm)',
defaultSubscription: 'type = "group"',
fields: {
groupKey: {
label: 'Group Key',
type: 'string',
required: false,
description:
'The group key you specified in Launchpad under the company corresponding to the group. If this is not specified, it will be defaulted to "$group_id". This is helpful when you have a group of companies that should be joined together as in when you have a multinational.'
},
groupId: {
label: 'Group ID',
type: 'string',
description:
'The unique identifier of the group. If there is a trait that matches the group key, it will override this value.',
required: true,
default: {
'@path': '$.groupId'
}
},
traits: {
label: 'Group Properties',
type: 'object',
description: 'The properties to set on the group profile.',
required: false,
default: {
'@path': '$.traits'
}
},
userId: {
label: 'User ID',
type: 'string',
description:
'A unique ID for a known user. This will be used as the Distinct ID. This field is required if the Anonymous ID field is empty',
default: {
'@path': '$.userId'
}
},
anonymousId: {
label: 'Anonymous ID',
type: 'string',
description:
'A unique ID for an anonymous user. This will be used as the Distinct ID if the User ID field is empty. This field is required if the User ID field is empty',
default: {
'@path': '$.anonymousId'
}
}
},

perform: async (request, { payload, settings }) => {
const groupId = payload.groupId
const apiServerUrl = getApiServerUrl(settings.apiRegion)
let transformed_traits

if (payload.traits) {
transformed_traits = {
...Object.fromEntries(Object.entries(payload.traits).map(([k, v]) => [`group_${k}`, v]))
}
}
const groupIdentifyEvent = {
event: '$identify',
type: 'screen',
$set: {
group_id: groupId,
...transformed_traits
},
distinct_id: payload.userId ? payload.userId : payload.anonymousId,
user_id: payload.userId,
anonymoud_id: payload.anonymousId,
api_key: settings.apiSecret
}
const groupIdentifyResponse = await request(`${apiServerUrl}capture`, {
method: 'post',
json: groupIdentifyEvent
})

return groupIdentifyResponse
}
}

export default groupIdentifyUser
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing snapshot for Launchpad's identifyUser destination action: all fields 1`] = `
Object {
"$ip": "5SpoCjYek1jtNwUC",
"$set": Object {
"testType": "5SpoCjYek1jtNwUC",
},
"anonymous_id": "5SpoCjYek1jtNwUC",
"api_key": "5SpoCjYek1jtNwUC",
"distinct_id": "5SpoCjYek1jtNwUC",
"event": "$identify",
"type": "screen",
"user_id": "5SpoCjYek1jtNwUC",
}
`;

exports[`Testing snapshot for Launchpad's identifyUser destination action: required fields 1`] = `
Object {
"$set": Object {
"testType": "5SpoCjYek1jtNwUC",
},
"api_key": "5SpoCjYek1jtNwUC",
"distinct_id": "user1234",
"event": "$identify",
"type": "screen",
"user_id": "user1234",
}
`;
Loading