Less magical custom hash mechanism.

Signed-off-by: Eric Peterson <i.am@eric.pe>
This commit is contained in:
Eric Peterson
2022-01-30 18:51:18 +01:00
parent 35d1d675b5
commit 919a77845d
5 changed files with 45 additions and 35 deletions
+19 -22
View File
@@ -128,35 +128,32 @@ enable this...
Note that, to comply with GA policies, the value of the User ID is
pseudonymized before being sent to GA. By default, it is a `sha256` hash of the
current user's `userEntityRef` as returned by the `identityApi`. To set a
different value, provide a custom implementation of the `identityApi` that
resolves a `userEntityRef` of the form `PrivateUser:namespace/YOUR-VALUE`. For
example:
different value, provide a `userIdTransform` function alongside `identityApi`
when you instantiate `GoogleAnalytics`. This function will be passed the
`userEntityRef` as an argument and should resolve to the value you wish to set
as the user ID. For example:
```typescript
import {
analyticsApiRef,
configApiRef,
identityApiRef,
} from '@backstage/core-plugin-api';
import { GoogleAnalytics } from '@backstage/plugin-analytics-module-ga';
export const apis: AnyApiFactory[] = [
createApiFactory({
api: analyticsApiRef,
deps: { config: configApiRef, identityApi: identityApiRef },
factory: ({ identityApi, config }) => {
return new PseudononymizedIdentity(identityApi);
},
deps: { configApi: configApiRef, identityApi: identityApiRef },
factory: ({ configApi, identityApi }) =>
GoogleAnalytics.fromConfig(configApi, {
identityApi,
userIdTransform: async (userEntityRef: string): Promise<string> => {
return customHashingFunction(userEntityRef);
},
}),
}),
];
class PseudononymizedIdentity implements IdentityApi {
constructor(private actualApi: IdentityApi) {}
async getBackstageIdentity(): Promise<BackstageUserIdentity> {
const { email = 'someone' } = await this.actualApi.getProfileInfo();
const hashedEmail = customHashingFunction(email);
return {
type: 'user',
userEntityRef: `PrivateUser:default/${hashedEmail}`,
ownershipEntityRefs: [],
};
}
// ...
}
```
### Debugging and Testing