This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #817 from solocommand/idx-idt-tracking
IdentityX identity tracking
- Loading branch information
Showing
13 changed files
with
219 additions
and
5 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
packages/marko-web-identity-x/api/queries/load-user-by-external-id.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const gql = require('graphql-tag'); | ||
const userFragment = require('../fragments/active-user'); | ||
|
||
module.exports = gql` | ||
query LoadUserByExternalId( | ||
$identifier: AppUserExternalIdentifierInput!, | ||
$namespace: AppUserExternalNamespaceInput! | ||
) { | ||
appUserByExternalId(input: { identifier: $identifier, namespace: $namespace }) { | ||
...ActiveUserFragment | ||
} | ||
} | ||
${userFragment} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/marko-web-omeda-identity-x/api/queries/customer-by-encrypted-id.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const gql = require('graphql-tag'); | ||
|
||
module.exports = gql` | ||
query IdXIdentifyCustomer($id: String!) { | ||
customerByEncryptedId(input: { id: $id, errorOnNotFound: false }) { | ||
id | ||
primaryEmailAddress { emailAddress } | ||
} | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Omeda+IdentityX Middleware | ||
=== | ||
|
||
## Set Identity Cookie | ||
This [middleware](./set-identity-cookie.js) sets the IdentityX Identity cookie. This cookie represents the identity that all user behavior should be attributed to. | ||
|
||
Test cases: | ||
1. Fully anonymous user (no omeda or idx tokens). No cookie should be set. | ||
2. Logged in user w/o cookie: cookkie should be set | ||
3. Logged in user w/ cookie: Cookie shoudl not be set. | ||
4. Omeda identity cookie present, identityx user exists for ext id: cookie shoul dbe set. | ||
5. Omeda identity cookie present, no identityx user exists: cookie should be set. |
73 changes: 73 additions & 0 deletions
73
packages/marko-web-omeda-identity-x/middleware/set-identity-cookie.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const { asyncRoute } = require('@parameter1/base-cms-utils'); | ||
const { get } = require('@parameter1/base-cms-object-path'); | ||
const olyticsCookie = require('@parameter1/base-cms-marko-web-omeda/olytics/customer-cookie'); | ||
const query = require('../api/queries/customer-by-encrypted-id'); | ||
|
||
/** | ||
* | ||
* @param {import('express').Request} req | ||
* @param {import('express').Response} res | ||
* @returns {String|null} | ||
*/ | ||
const findOlyticsId = (req, res) => { | ||
// Read from request cookies | ||
const reqId = olyticsCookie.parseFrom(req); | ||
if (reqId) return reqId; | ||
// request query param | ||
const { oly_enc_id: qId } = req.query; | ||
if (qId) return qId; | ||
// response cookie | ||
const cookies = (res.get('set-cookie') || []).reduce((o, c) => { | ||
const [r] = `${c}`.split(';'); | ||
const [k, v] = `${r}`.split('='); | ||
return { ...o, [k]: v }; | ||
}, {}); | ||
const resId = olyticsCookie.parseFrom({ cookies }); | ||
return resId; | ||
}; | ||
|
||
/** | ||
* @typedef OIDXRequest | ||
* @prop {import('@parameter1/omeda-graphql-client')} $omedaGraphQLClient | ||
* @prop {import('@parameter1/base-cms-marko-web-identity-x/service')} identityX | ||
* | ||
* @param {Object} o | ||
* @param {String} o.brandKey | ||
*/ | ||
module.exports = ({ | ||
brandKey, | ||
}) => asyncRoute(async (req, res, next) => { | ||
/** @type {OIDXRequest} */ | ||
const { identityX: idx, $omedaGraphQLClient: omeda } = req; | ||
const cookie = idx.getIdentity(res); | ||
|
||
// Don't overwrite an existing cookie | ||
if (cookie) return next(); | ||
|
||
// get oly enc id. if we don't have one, bail | ||
const omedaId = findOlyticsId(req, res); | ||
if (!omedaId) return next(); | ||
|
||
// Look up idx user by encrypted id | ||
const namespace = { provider: 'omeda', tenant: brandKey.toLowerCase(), type: 'customer' }; | ||
const identity = await idx.findUserByExternalId({ identifier: omedaId, namespace }); | ||
if (identity) { | ||
idx.setIdentityCookie(identity.id); | ||
return next(); | ||
} | ||
|
||
const or = await omeda.query({ query, variables: { id: omedaId } }); | ||
const email = get(or, 'data.customerByEncryptedId.primaryEmailAddress.emailAddress'); | ||
if (email) { | ||
// Upsert the user and add the external id to it. | ||
const { id } = await idx.createAppUser({ email }); | ||
await idx.addExternalUserId({ | ||
userId: id, | ||
identifier: { value: omedaId, type: 'encrypted' }, | ||
namespace, | ||
}); | ||
idx.setIdentityCookie(id); | ||
return next(); | ||
} | ||
return next(); | ||
}); |
18 changes: 18 additions & 0 deletions
18
packages/marko-web-theme-monorail/components/identity-x/identify.marko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
$ const { req, res } = out.global; | ||
$ const { identityX } = req; | ||
$ const identity = identityX.getIdentity(res); | ||
|
||
<if(Boolean(identityX))> | ||
<marko-web-identity-x-context|{ user, hasUser }|> | ||
$ const payload = { | ||
...(identity && { identity }), | ||
...(hasUser && { | ||
user: identityX.config.getGTMUserData(user), | ||
user_id: user.id | ||
}), | ||
}; | ||
<if(hasUser || identity)> | ||
<marko-web-gtm-push data=payload /> | ||
</if> | ||
</marko-web-identity-x-context> | ||
</if> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters