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

feat(quantic): added logic to include quantic version in analytic payloads #4685

Merged
merged 12 commits into from
Nov 26, 2024
1 change: 1 addition & 0 deletions packages/quantic/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ force-app/main/default/staticresources/coveoheadless
force-app/main/default/staticresources/coveobueno
force-app/main/default/staticresources/marked
force-app/main/default/staticresources/dompurify
force-app/main/default/staticresources/quanticMetadata.json

# Test Coverage & Report
coverage
Expand Down
141 changes: 141 additions & 0 deletions packages/quantic/build-static-resources.js
mmitiche marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
const {promisify} = require('util');
const ncp = promisify(require('ncp'));
const fs = require('fs').promises;
const path = require('path');

const staticResourcesPath = './force-app/main/default/staticresources';

async function getPackageVersion() {
const packageJsonPath = path.join(__dirname, 'package.json');
return JSON.parse(await fs.readFile(packageJsonPath, 'utf8')).version;
}

const copy = async (source, dest) => {
try {
return await ncp(source, dest);
} catch (e) {
console.log(`Failed to copy: ${source}\nDoes the resource exist?`);
process.exit(1);
}
};

const main = async () => {
console.info('Begin building static resources');
await copyHeadless();
await copyBueno();
await copyMarked();
await copyDompurify();
await setupQuanticMetadata();
};

const copyDompurify = async () => {
console.info('Begin copy DOMPurify.');

await fs.mkdir(`${staticResourcesPath}/dompurify`, {
recursive: true,
});

await copy(
'../../node_modules/dompurify/dist/purify.min.js',
`${staticResourcesPath}/dompurify/purify.min.js`
);

console.info('DOMPurify copied.');
};

const copyMarked = async () => {
console.info('Begin copy Marked.');

await fs.mkdir(`${staticResourcesPath}/marked`, {
recursive: true,
});

await copy(
'../../node_modules/marked/marked.min.js',
`${staticResourcesPath}/marked/marked.min.js`
);

console.info('Marked copied.');
};

const copyHeadless = async () => {
console.info('Begin copy Headless.');

await fs.mkdir(`${staticResourcesPath}/coveoheadless/case-assist`, {
recursive: true,
});
await fs.mkdir(`${staticResourcesPath}/coveoheadless/insight`, {
recursive: true,
});
await fs.mkdir(`${staticResourcesPath}/coveoheadless/recommendation`, {
recursive: true,
});
await fs.mkdir(`${staticResourcesPath}/coveoheadless/definitions/`, {
recursive: true,
});
await copy(
'.tmp/quantic-compiled/headless.js',
`${staticResourcesPath}/coveoheadless/headless.js`
);
await copy(
'.tmp/quantic-compiled/case-assist/headless.js',
`${staticResourcesPath}/coveoheadless/case-assist/headless.js`
);
await copy(
'.tmp/quantic-compiled/insight/headless.js',
`${staticResourcesPath}/coveoheadless/insight/headless.js`
);
await copy(
'.tmp/quantic-compiled/recommendation/headless.js',
`${staticResourcesPath}/coveoheadless/recommendation/headless.js`
);
await copy(
'../../node_modules/@coveo/headless/dist/definitions',
`${staticResourcesPath}/coveoheadless/definitions`
);

await fs.rm('.tmp', {recursive: true});

console.info('Headless copied.');
};

const copyBueno = async () => {
console.info('Begin copy Bueno.');

await fs.mkdir(`${staticResourcesPath}/coveobueno/browser`, {
recursive: true,
});
await fs.mkdir(`${staticResourcesPath}/coveobueno/definitions`, {
recursive: true,
});
await copy(
'../../node_modules/@coveo/bueno/dist/browser/bueno.js',
`${staticResourcesPath}/coveobueno/browser/bueno.js`
);
await copy(
'../../node_modules/@coveo/bueno/dist/definitions',
`${staticResourcesPath}/coveobueno/definitions`
);

console.info('Bueno copied.');
};

const setupQuanticMetadata = async () => {
try {
console.info('Begin Quantic metadata creation');
const outputJsonPath = `${staticResourcesPath}/quanticMetadata.json`;

const version = await getPackageVersion();
const metadata = {
version: version,
};
fs.writeFile(outputJsonPath, JSON.stringify(metadata, null, 2), 'utf8');
console.info('Quantic metadata created.');
} catch (error) {
console.error('Error occurred Quantic metadata creation: ', error);
}
};

main().then(() => {
console.info('Copy done!');
});
124 changes: 0 additions & 124 deletions packages/quantic/copy-static-resources.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
HeadlessBundleNames,
} from 'c/quanticHeadlessLoader';
import {LightningElement, api} from 'lwc';
import quanticMetadata from '@salesforce/resourceUrl/quanticMetadata';

/** @typedef {import("coveo").CaseAssistEngine} CaseAssistEngine */
/** @typedef {import("coveo").CaseAssistEngineOptions} CaseAssistEngineOptions */
Expand Down Expand Up @@ -49,10 +50,19 @@ export default class QuanticCaseAssistInterface extends LightningElement {
loadDependencies(this, HeadlessBundleNames.caseAssist)
.then(() => {
if (!getHeadlessBindings(this.engineId)?.engine) {
getHeadlessConfiguration()
const beforeInitPromises = [
getHeadlessConfiguration(),
fetch(quanticMetadata).then((response) => response.json()),
];

Promise.all(beforeInitPromises)
.then((data) => {
if (data) {
const {organizationId, accessToken, ...rest} = JSON.parse(data);
const {organizationId, accessToken, ...rest} = JSON.parse(
data[0]
);
const {version: quanticVersion} = data[1];

this.engineOptions = {
configuration: {
organizationId,
Expand All @@ -64,6 +74,13 @@ export default class QuanticCaseAssistInterface extends LightningElement {
...(document.referrer && {
originLevel3: document.referrer.substring(0, 256),
}),
analyticsClientMiddleware: (_event, payload) => {
if (!payload.customData) {
payload.customData = {};
}
payload.customData.coveoQuanticVersion = quanticVersion;
return payload;
},
},
...rest,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
setInitializedCallback,
} from 'c/quanticHeadlessLoader';
import {LightningElement, api} from 'lwc';
import quanticMetadata from '@salesforce/resourceUrl/quanticMetadata';

/** @typedef {import("coveo").InsightEngine} InsightEngine */
/** @typedef {import("coveo").InsightEngineOptions} InsightEngineOptions */
Expand Down Expand Up @@ -63,10 +64,19 @@ export default class QuanticInsightInterface extends LightningElement {
loadDependencies(this, HeadlessBundleNames.insight)
.then(() => {
if (!getHeadlessBindings(this.engineId)?.engine) {
getHeadlessConfiguration()
const beforeInitPromises = [
getHeadlessConfiguration(),
fetch(quanticMetadata).then((response) => response.json()),
];

Promise.all(beforeInitPromises)
.then((data) => {
if (data) {
const {organizationId, accessToken, ...rest} = JSON.parse(data);
const {organizationId, accessToken, ...rest} = JSON.parse(
data[0]
);
const {version: quanticVersion} = data[1];

this.engineOptions = {
configuration: {
organizationId,
Expand All @@ -80,6 +90,13 @@ export default class QuanticInsightInterface extends LightningElement {
...(document.referrer && {
originLevel3: document.referrer.substring(0, 256),
}),
analyticsClientMiddleware: (_event, payload) => {
if (!payload.customData) {
payload.customData = {};
}
payload.customData.coveoQuanticVersion = quanticVersion;
return payload;
},
},
...rest,
},
Expand Down
Loading
Loading