@segment/analytics-browser-actions-jimo 1.0.1-onSubscriptionSave.1695

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # @segment/analytics-browser-actions-jimo
2
+
3
+ The Jimo browser action destination for use with @segment/analytics-next.
4
+
5
+ ## License
6
+
7
+ MIT License
8
+
9
+ Copyright (c) 2023 Segment
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+
29
+ ## Contributing
30
+
31
+ 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.
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@segment/analytics-browser-actions-jimo",
3
+ "version": "1.0.1-onSubscriptionSave.1695+c605951c",
4
+ "license": "MIT",
5
+ "publishConfig": {
6
+ "access": "public",
7
+ "registry": "https://registry.npmjs.org"
8
+ },
9
+ "main": "./dist/cjs",
10
+ "module": "./dist/esm",
11
+ "scripts": {
12
+ "build": "yarn build:esm && yarn build:cjs",
13
+ "build:cjs": "tsc --module commonjs --outDir ./dist/cjs",
14
+ "build:esm": "tsc --outDir ./dist/esm"
15
+ },
16
+ "typings": "./dist/esm",
17
+ "dependencies": {
18
+ "@segment/browser-destination-runtime": "^1.14.1-onSubscriptionSave.50+c605951c"
19
+ },
20
+ "peerDependencies": {
21
+ "@segment/analytics-next": ">=1.55.0"
22
+ },
23
+ "gitHead": "c605951c8cd226a806a2da74654cdefbcda1f5f4"
24
+ }
@@ -0,0 +1,12 @@
1
+ // Generated file. DO NOT MODIFY IT BY HAND.
2
+
3
+ export interface Settings {
4
+ /**
5
+ * Id of the Jimo project. You can find it here: https://i.usejimo.com/settings/install/portal
6
+ */
7
+ projectId: string
8
+ /**
9
+ * Make sure Jimo is not initialized automatically after being added to your website. For more information, check out: https://help.usejimo.com/knowledge-base/for-developers/sdk-guides/manual-initialization
10
+ */
11
+ manualInit?: boolean
12
+ }
package/src/index.ts ADDED
@@ -0,0 +1,63 @@
1
+ import { defaultValues } from '@segment/actions-core'
2
+ import { browserDestination } from '@segment/browser-destination-runtime/shim'
3
+ import type { BrowserDestinationDefinition } from '@segment/browser-destination-runtime/types'
4
+ import type { Settings } from './generated-types'
5
+ import { initScript } from './init-script'
6
+ import sendUserData from './sendUserData'
7
+ import { JimoSDK } from './types'
8
+
9
+ declare global {
10
+ interface Window {
11
+ jimo: JimoSDK | never[]
12
+ JIMO_PROJECT_ID: string
13
+ JIMO_MANUAL_INIT: boolean
14
+ }
15
+ }
16
+
17
+ const ENDPOINT_UNDERCITY = 'https://undercity.usejimo.com/jimo-invader.js'
18
+
19
+ export const destination: BrowserDestinationDefinition<Settings, JimoSDK> = {
20
+ name: 'Jimo (Actions)',
21
+ slug: 'actions-jimo',
22
+ mode: 'device',
23
+ description: 'Load Jimo SDK and send user profile data to Jimo',
24
+
25
+ settings: {
26
+ projectId: {
27
+ description:
28
+ 'Id of the Jimo project. You can find the Project Id here: https://i.usejimo.com/settings/install/portal',
29
+ label: 'Id',
30
+ type: 'string',
31
+ required: true
32
+ },
33
+ manualInit: {
34
+ label: 'Initialize Jimo manually',
35
+ description:
36
+ 'Toggling to true will prevent Jimo from initializing automatically. For more information, check out: https://help.usejimo.com/knowledge-base/for-developers/sdk-guides/manual-initialization',
37
+ type: 'boolean',
38
+ required: false,
39
+ default: false
40
+ }
41
+ },
42
+ presets: [
43
+ {
44
+ name: 'Send User Data',
45
+ subscribe: 'type = "identify"',
46
+ partnerAction: 'sendUserData',
47
+ mapping: defaultValues(sendUserData.fields),
48
+ type: 'automatic'
49
+ }
50
+ ],
51
+ initialize: async ({ settings }, deps) => {
52
+ initScript(settings)
53
+
54
+ await deps.loadScript(`${ENDPOINT_UNDERCITY}`)
55
+
56
+ return window.jimo as JimoSDK
57
+ },
58
+ actions: {
59
+ sendUserData
60
+ }
61
+ }
62
+
63
+ export default browserDestination(destination)
@@ -0,0 +1,12 @@
1
+ import { Settings } from './generated-types'
2
+ /* eslint-disable */
3
+ // @ts-nocheck
4
+ export function initScript(settings: Settings) {
5
+ if (window.jimo) {
6
+ return
7
+ }
8
+
9
+ window.jimo = []
10
+ window['JIMO_PROJECT_ID'] = settings.projectId
11
+ window['JIMO_MANUAL_INIT'] = settings.manualInit === true
12
+ }
@@ -0,0 +1,50 @@
1
+ import { Analytics, Context } from '@segment/analytics-next'
2
+
3
+ import sendUserData from '..'
4
+ import { JimoSDK } from '../../types'
5
+ import { Payload } from '../generated-types'
6
+
7
+ describe('Jimo - Send User Data', () => {
8
+ test('user id', async () => {
9
+ const client = {
10
+ push: jest.fn()
11
+ } as any as JimoSDK
12
+
13
+ const context = new Context({
14
+ type: 'identify'
15
+ })
16
+
17
+ await sendUserData.perform(client as any as JimoSDK, {
18
+ settings: { projectId: 'unk' },
19
+ analytics: jest.fn() as any as Analytics,
20
+ context: context,
21
+ payload: {
22
+ userId: 'u1'
23
+ } as Payload
24
+ })
25
+
26
+ expect(client.push).toHaveBeenCalled()
27
+ expect(client.push).toHaveBeenCalledWith(['set', 'user:id', ['u1']])
28
+ })
29
+ test('user email', async () => {
30
+ const client = {
31
+ push: jest.fn()
32
+ } as any as JimoSDK
33
+
34
+ const context = new Context({
35
+ type: 'identify'
36
+ })
37
+
38
+ await sendUserData.perform(client as any as JimoSDK, {
39
+ settings: { projectId: 'unk' },
40
+ analytics: jest.fn() as any as Analytics,
41
+ context: context,
42
+ payload: {
43
+ email: 'foo@bar.com'
44
+ } as Payload
45
+ })
46
+
47
+ expect(client.push).toHaveBeenCalled()
48
+ expect(client.push).toHaveBeenCalledWith(['set', 'user:email', ['foo@bar.com']])
49
+ })
50
+ })
@@ -0,0 +1,12 @@
1
+ // Generated file. DO NOT MODIFY IT BY HAND.
2
+
3
+ export interface Payload {
4
+ /**
5
+ * The users's id provided by segment
6
+ */
7
+ userId?: string | null
8
+ /**
9
+ * The email of the user
10
+ */
11
+ email?: string | null
12
+ }
@@ -0,0 +1,45 @@
1
+ import type { BrowserActionDefinition } from '@segment/browser-destination-runtime/types'
2
+ import { JimoSDK } from 'src/types'
3
+ import type { Settings } from '../generated-types'
4
+ import type { Payload } from './generated-types'
5
+
6
+ const action: BrowserActionDefinition<Settings, JimoSDK, Payload> = {
7
+ title: 'Send User Data',
8
+ description: 'Send user ID and email to Jimo',
9
+ platform: 'web',
10
+ fields: {
11
+ userId: {
12
+ label: 'User ID',
13
+ description: 'The unique user identifier',
14
+ type: 'string',
15
+ allowNull: true,
16
+ required: false,
17
+ default: {
18
+ '@path': '$.userId'
19
+ }
20
+ },
21
+ email: {
22
+ label: 'User email',
23
+ description: 'The email of the user',
24
+ type: 'string',
25
+ allowNull: true,
26
+ required: false,
27
+ default: {
28
+ '@path': '$.traits.email'
29
+ }
30
+ }
31
+ },
32
+ defaultSubscription: 'type = "identify"',
33
+ perform: (jimo, { payload }) => {
34
+ if (payload.userId != null) {
35
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
36
+ jimo.push(['set', 'user:id', [payload.userId]])
37
+ }
38
+ if (payload.email != null) {
39
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
40
+ jimo.push(['set', 'user:email', [payload.email]])
41
+ }
42
+ }
43
+ }
44
+
45
+ export default action
package/src/types.ts ADDED
@@ -0,0 +1,3 @@
1
+ export interface JimoSDK {
2
+ push: (params: Array<string | string[]>) => Promise<void>
3
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.build.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "baseUrl": "."
6
+ },
7
+ "include": ["src"],
8
+ "exclude": ["dist", "**/__tests__"]
9
+ }