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

fix(editor): Update FE telemetry for RBAC #9343

Merged
merged 6 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ export default defineComponent({
this.$telemetry.track('User duplicated workflow', {
old_workflow_id: currentWorkflowId,
workflow_id: this.data.id,
// Hardcoding a value as this needs to be updated in the future anyways
sharing_role: 'unknown',
sharing_role: this.workflowHelpers.getWorkflowProjectRole(this.data.id),
});
}
} catch (error) {
Expand Down
4 changes: 3 additions & 1 deletion packages/editor-ui/src/components/Telemetry.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useRootStore } from '@/stores/n8nRoot.store';
import { useSettingsStore } from '@/stores/settings.store';
import { useUsersStore } from '@/stores/users.store';
import type { ITelemetrySettings } from 'n8n-workflow';
import { useProjectsStore } from '@/features/projects/projects.store';

export default defineComponent({
name: 'Telemetry',
Expand All @@ -18,7 +19,7 @@ export default defineComponent({
};
},
computed: {
...mapStores(useRootStore, useSettingsStore, useUsersStore),
...mapStores(useRootStore, useSettingsStore, useUsersStore, useProjectsStore),
currentUserId(): string {
return this.usersStore.currentUserId || '';
},
Expand Down Expand Up @@ -62,6 +63,7 @@ export default defineComponent({
this.$telemetry.init(this.telemetry, {
instanceId: this.rootStore.instanceId,
userId: this.currentUserId,
projectId: this.projectsStore.personalProject?.id,
versionCli: this.rootStore.versionCli,
});

Expand Down
21 changes: 21 additions & 0 deletions packages/editor-ui/src/components/WorkflowShareModal.ee.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
:readonly="!workflowPermissions.share"
:static="isHomeTeamProject"
:placeholder="$locale.baseText('workflows.shareModal.select.placeholder')"
@project-added="onProjectAdded"
@project-removed="onProjectRemoved"
/>
<n8n-info-tip v-if="isHomeTeamProject" :bold="false" class="mt-s">
<i18n-t keypath="workflows.shareModal.info.members" tag="span">
Expand Down Expand Up @@ -143,6 +145,7 @@ import { useUIStore } from '@/stores/ui.store';
import { useUsersStore } from '@/stores/users.store';
import { useWorkflowsStore } from '@/stores/workflows.store';
import { useWorkflowsEEStore } from '@/stores/workflows.ee.store';
import type { ITelemetryTrackProperties } from 'n8n-workflow';
import type { BaseTextKey } from '@/plugins/i18n';
import { isNavigationFailure } from 'vue-router';
import ProjectSharing from '@/features/projects/components/ProjectSharing.vue';
Expand Down Expand Up @@ -262,6 +265,18 @@ export default defineComponent({
await this.initialize();
},
methods: {
onProjectAdded(project: ProjectSharingData) {
this.trackTelemetry('User selected sharee to add', {
project_id_sharer: this.workflow.homeProject?.id,
project_id_sharee: project.id,
});
},
onProjectRemoved(project: ProjectSharingData) {
this.trackTelemetry('User selected sharee to remove', {
project_id_sharer: this.workflow.homeProject?.id,
project_id_sharee: project.id,
});
},
async onSave() {
if (this.loading) {
return;
Expand Down Expand Up @@ -331,6 +346,12 @@ export default defineComponent({
});
this.modalBus.emit('close');
},
trackTelemetry(eventName: string, data: ITelemetryTrackProperties) {
this.$telemetry.track(eventName, {
workflow_id: this.workflow.id,
...data,
});
},
goToUpgrade() {
void this.uiStore.goToUpgrade('workflow_sharing', 'upgrade-workflow-sharing');
},
Expand Down
19 changes: 18 additions & 1 deletion packages/editor-ui/src/composables/useWorkflowHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ export function useWorkflowHelpers(options: { router: ReturnType<typeof useRoute
if (error.errorCode === 100) {
telemetry.track('User attempted to save locked workflow', {
workflowId: currentWorkflow,
sharing_role: workflowPermissions.value.isOwner ? 'owner' : 'sharee',
sharing_role: getWorkflowProjectRole(currentWorkflow),
});

const url = router.resolve({
Expand Down Expand Up @@ -1182,6 +1182,22 @@ export function useWorkflowHelpers(options: { router: ReturnType<typeof useRoute
});
}

function getWorkflowProjectRole(workflowId: string): 'owner' | 'sharee' | 'member' {
const workflow = workflowsStore.workflowsById[workflowId];

if (workflow.homeProject?.id === projectsStore.personalProject?.id) {
return 'owner';
} else if (
workflow.sharedWithProjects?.some(
(project) => project.id === projectsStore.personalProject?.id,
)
) {
return 'sharee';
} else {
return 'member';
}
}

return {
resolveParameter,
resolveRequiredParameters,
Expand All @@ -1207,5 +1223,6 @@ export function useWorkflowHelpers(options: { router: ReturnType<typeof useRoute
dataHasChanged,
removeForeignCredentialsFromWorkflow,
workflowPermissions,
getWorkflowProjectRole,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const props = defineProps<Props>();
const model = defineModel<(ProjectSharingData | null) | ProjectSharingData[]>({
required: true,
});
const emit = defineEmits<{
(event: 'projectAdded', value: ProjectSharingData): void;
(event: 'projectRemoved', value: ProjectSharingData): void;
}>();

const selectedProject = ref(Array.isArray(model.value) ? '' : model.value?.id ?? '');
const filter = ref('');
Expand Down Expand Up @@ -61,6 +65,7 @@ const onProjectSelected = (projectId: string) => {
} else {
model.value = project;
}
emit('projectAdded', project);
};

const onRoleAction = (project: ProjectSharingData, role: string) => {
Expand All @@ -75,6 +80,7 @@ const onRoleAction = (project: ProjectSharingData, role: string) => {

if (role === 'remove') {
model.value = model.value.filter((p) => p.id !== project.id);
emit('projectRemoved', project);
}
};

Expand Down
24 changes: 24 additions & 0 deletions packages/editor-ui/src/plugins/__tests__/telemetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ describe('telemetry', () => {
});
});

it('Rudderstack identify method should be called when proving userId and versionCli and projectId', () => {
const identifyFunction = vi.spyOn(window.rudderanalytics, 'identify');

const userId = '1';
const instanceId = '1';
const versionCli = '1';
const projectId = '1';

settingsStore.setSettings(
merge({}, SETTINGS_STORE_DEFAULT_STATE.settings, {
deployment: {
type: '',
},
}),
);

telemetry.identify(userId, instanceId, versionCli, projectId);
expect(identifyFunction).toHaveBeenCalledTimes(1);
expect(identifyFunction).toHaveBeenCalledWith(`${instanceId}#${userId}#${projectId}`, {
instance_id: instanceId,
version_cli: versionCli,
});
});

it('Rudderstack identify method should be called when proving userId and deployment type is cloud ', () => {
const identifyFunction = vi.spyOn(window.rudderanalytics, 'identify');

Expand Down
11 changes: 8 additions & 3 deletions packages/editor-ui/src/plugins/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ export class Telemetry {
{
instanceId,
userId,
projectId,
versionCli,
}: {
instanceId: string;
userId?: string;
projectId?: string;
versionCli: string;
},
) {
Expand All @@ -73,13 +75,13 @@ export class Telemetry {
});
useTelemetryStore().init(this);

this.identify(instanceId, userId, versionCli);
this.identify(instanceId, userId, versionCli, projectId);

this.flushPageEvents();
this.track('Session started', { session_id: rootStore.pushRef });
}

identify(instanceId: string, userId?: string, versionCli?: string) {
identify(instanceId: string, userId?: string, versionCli?: string, projectId?: string) {
const settingsStore = useSettingsStore();
const traits: { instance_id: string; version_cli?: string; user_cloud_id?: string } = {
instance_id: instanceId,
Expand All @@ -90,7 +92,10 @@ export class Telemetry {
traits.user_cloud_id = settingsStore.settings?.n8nMetadata?.userId ?? '';
}
if (userId) {
this.rudderStack.identify(`${instanceId}#${userId}`, traits);
this.rudderStack.identify(
`${instanceId}#${userId}${projectId ? '#' + projectId : ''}`,
traits,
);
} else {
this.rudderStack.reset();
}
Expand Down