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

create trackEvent and identifyUser browser-destinations for 1flow #1703

Merged
merged 8 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
31 changes: 31 additions & 0 deletions packages/browser-destinations/destinations/1flow/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# @segment/analytics-browser-actions-1flow

The 1Flow browser action destination for use with @segment/analytics-next.

## License

MIT License

Copyright (c) 2023 Segment

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

## Contributing

All third party contributors acknowledge that any contributions they provide will be made under the same open source license that the open source project is provided under.
24 changes: 24 additions & 0 deletions packages/browser-destinations/destinations/1flow/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@segment/analytics-browser-actions-1flow",
"version": "1.0.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/segmentio/action-destinations",
"directory": "packages/browser-destinations/destinations/1flow"
},
"main": "./dist/cjs",
"module": "./dist/esm",
"scripts": {
"build": "yarn build:esm && yarn build:cjs",
"build:cjs": "tsc --module commonjs --outDir ./dist/cjs",
"build:esm": "tsc --outDir ./dist/esm"
},
"typings": "./dist/esm",
"dependencies": {
"@segment/browser-destination-runtime": "^1.4.0"
},
"peerDependencies": {
"@segment/analytics-next": ">=1.55.0"
}
}
22 changes: 22 additions & 0 deletions packages/browser-destinations/destinations/1flow/src/1flow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable */
// @ts-nocheck

export function initScript({ projectApiKey }) {
//Set your APP_ID
const apiKey = projectApiKey

const autoURLTracking = false
;(function (w, o, s, t, k, a, r) {
;(w._1flow = function (e, d, v) {
s(function () {
w._1flow(e, d, !v ? {} : v)
}, 5)
}),
(a = o.getElementsByTagName('head')[0])
r = o.createElement('script')
r.async = 1
r.setAttribute('data-api-key', k)
r.src = t
a.appendChild(r)
})(window, document, setTimeout, 'https://cdn-development.1flow.ai/js-sdk/1flow.js', apiKey)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import { Subscription } from '@segment/browser-destination-runtime/types'
import { Analytics, Context } from '@segment/analytics-next'
import _1FlowDestination, { destination } from '../index'

const subscriptions: Subscription[] = [
{
name: 'Identify user',
subscribe: 'type = "identify"',
partnerAction: 'identify',
enabled: true,
mapping: {
userId: {
type: 'string',
required: true,
label: 'User ID',
description: 'Unique user ID',
default: {
'@path': '$.userId'
}
},
anonymousId: {
type: 'string',
required: false,
description: 'Anonymous id of the user',
label: 'Anonymous ID',
default: {
'@path': '$.anonymousId'
}
},
traits: {
type: 'object',
required: false,
description: 'User traits used to enrich user identification',
label: 'Traits',
default: {
'@path': '$.traits'
}
}, first_name: {
description: "The user's first name.",
label: 'First Name',
type: 'string',
required: false,
default: {
'@path': '$.traits.first_name'
}
},
last_name: {
description: "The user's last name.",
label: 'First Name',
type: 'string',
required: false,
default: {
'@path': '$.traits.last_name'
}
},
phone: {
description: "The user's phone number.",
label: 'Phone Number',
type: 'string',
required: false,
default: {
'@path': '$.traits.phone'
}
},

email: {
description: "The user's email address.",
label: 'Email Address',
type: 'string',
required: false,
default: {
'@path': '$.traits.email'
}
}
}
},
{
name: 'Track event',
subscribe: 'type = "track"',
partnerAction: 'track',
enabled: true,
mapping: {
event_name: {
description: 'The name of the event.',
label: 'Event Name',
type: 'string',
required: true,
default: {
'@path': '$.event'
}
},
properties: {
description: 'Information associated with the event',
label: 'Event Properties',
type: 'object',
required: false,
default: {
'@path': '$.properties'
}
},
user_id: {
description: 'A unique identifier for the user.',
label: 'User ID',
type: 'string',
required: false,
default: {
'@path': '$.userId'
}
},
anonymous_id: {
description: "An anonymous identifier for the user.",
label: 'Anonymous ID',
type: 'string',
required: false,
default: {
'@path': '$.anonymousId'
}
},
}
}
]

describe('_1Flow', () => {
beforeAll(() => {
jest.mock('@segment/browser-destination-runtime/load-script', () => ({
loadScript: (_src: any, _attributes: any) => { }
}))
jest.mock('@segment/browser-destination-runtime/resolve-when', () => ({
resolveWhen: (_fn: any, _timeout: any) => { }
}))
})

const testID = 'testId'

test('load 1Flow SDK', async () => {
const [event] = await _1FlowDestination({
id: testID,
subscriptions
})

jest.spyOn(destination, 'initialize')
await event.load(Context.system(), {} as Analytics)
expect(destination.initialize).toHaveBeenCalled()

const scripts = window.document.querySelectorAll('script')
expect(scripts).toMatchSnapshot(`
<script
type="text/javascript"
src="https://cdn-development.1flow.ai/js-sdk/${testID}.js"
async=""
status="loaded">
</script>
`)
})
})



11 changes: 11 additions & 0 deletions packages/browser-destinations/destinations/1flow/src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type method = 'track' | 'identify'

type _1FlowApi = {
richLinkProperties: string[] | undefined
activator: string | undefined
projectApiKey: string
}

type _1FlowFunction = (method: method, ...args: unknown[]) => void

export type _1Flow = _1FlowFunction & _1FlowApi

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,90 @@
import { Analytics, Context } from '@segment/analytics-next'
import _1FlowDestination, { destination } from '../../index'
import { Subscription } from '@segment/browser-destination-runtime/types'

const subscriptions: Subscription[] = [
{
partnerAction: 'identify',
name: 'Identify User',
enabled: true,
subscribe: 'type = "identify"',
mapping: {
anonymousId: {
'@path': '$.anonymousId'
},
userId: {
'@path': '$.userId'
},
first_name: {
'@path': '$.traits.first_name'
},
last_name: {
'@path': '$.traits.last_name'
},
phone: {
'@path': '$.traits.phone'
},
email: {
'@path': '$.traits.email'
},
traits: {
'@path': '$.traits'
}
}
}
]

describe('identify', () => {
beforeAll(() => {
jest.mock('@segment/browser-destination-runtime/load-script', () => ({
loadScript: (_src: any, _attributes: any) => {}
}))
jest.mock('@segment/browser-destination-runtime/resolve-when', () => ({
resolveWhen: (_fn: any, _timeout: any) => {}
}))
})

let identify: any
const mockIdentify: jest.Mock<any, any> = jest.fn()

beforeEach(async () => {
const [_1FlowIdentify] = await _1FlowDestination({
id: 'testID',
subscriptions
})

identify = _1FlowIdentify

jest.spyOn(destination, 'initialize').mockImplementation(() => {
const mockedWithTrack = {
id: 'testID',
initialized: true,
emitter: { setSource: jest.fn() },
track: mockIdentify,
identify: jest.fn(),
setSource: jest.fn()
}
return Promise.resolve(mockedWithTrack)
})
await identify.load(Context.system(), {} as Analytics)
})

test('it maps event parameters correctly to identify function ', async () => {
jest.spyOn(destination.actions.identify, 'perform')
await identify.load(Context.system(), {} as Analytics)

await identify.identify?.(
new Context({
type: 'identify',
anonymousId: 'anon-123',
userId: 'some-user-123',
traits: {
first_name:"john",
last_name:"ayub",
phone: 123,
email: '[email protected]'
}
})
)
})
})

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

Loading