Skip to content

Commit 896e26a

Browse files
author
strausr
committed
feat(analytics): add test to detect cli
1 parent ad0c21f commit 896e26a

4 files changed

Lines changed: 47 additions & 9 deletions

File tree

packages/html/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type PictureSources = {minWidth?: number, maxWidth?: number, image: Cloud
2424

2525
export type PictureSource = {minWidth?: number, maxWidth?: number, image: CloudinaryImage, sizes?: string};
2626

27-
export type BaseAnalyticsOptions = {sdkSemver: string, techVersion: string, sdkCode: string, feature?: string};
27+
export type BaseAnalyticsOptions = {sdkSemver: string, techVersion: string, sdkCode: string, feature?: string, product?: string};
2828

2929
export type AnalyticsOptions = Parameters<CloudinaryImage['toURL']>[0];
3030

packages/html/src/utils/analytics.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const getAnalyticsOptions = (options?: BaseAnalyticsOptions, features: vo
77
sdkSemver: options.sdkSemver,
88
techVersion: options.techVersion,
99
...(options.feature !== undefined && { feature: options.feature }),
10+
...(options.product !== undefined && { product: options.product }),
1011
...features
1112
}
1213
} : null

packages/react/__tests__/analytics.test.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,40 @@ describe('analytics', () => {
2626
}, 0);// one tick
2727
});
2828
});
29+
30+
describe('analytics when created via CLI', () => {
31+
let AdvancedImageCLI: typeof AdvancedImage;
32+
let CloudinaryImageCLI: typeof CloudinaryImage;
33+
34+
beforeAll(() => {
35+
process.env.CLOUDINARY_SOURCE = 'cli';
36+
jest.resetModules();
37+
const src = require('../src');
38+
const constants = require('../src/internal/SDKAnalyticsConstants');
39+
AdvancedImageCLI = src.AdvancedImage;
40+
CloudinaryImageCLI = require('@cloudinary/url-gen/assets/CloudinaryImage').CloudinaryImage;
41+
const SDKAnalyticsConstantsCLI = constants.SDKAnalyticsConstants;
42+
SDKAnalyticsConstantsCLI.sdkSemver = '1.0.0';
43+
SDKAnalyticsConstantsCLI.techVersion = '10.2.5';
44+
});
45+
46+
afterAll(() => {
47+
delete process.env.CLOUDINARY_SOURCE;
48+
jest.resetModules();
49+
});
50+
51+
it('generates analytics with Product B (Integrations) and sdkCode H (React CLI)', function (done) {
52+
const cldImg = new CloudinaryImageCLI('sample', { cloudName: 'demo' });
53+
const component = mount(<AdvancedImageCLI cldImg={cldImg} />);
54+
setTimeout(() => {
55+
const html = component.html();
56+
const match = html.match(/_a=([A-Za-z0-9]+)/);
57+
expect(match).toBeTruthy();
58+
const token = match![1];
59+
// Algorithm B: 1st = algo, 2nd = product (B = Integrations), 3rd = sdkCode (H = React CLI)
60+
expect(token[1]).toBe('B');
61+
expect(token[2]).toBe('H');
62+
done();
63+
}, 0);
64+
});
65+
});
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import React from 'react'
22

3-
// Detect if this project was created via create-cloudinary-react CLI
4-
function getCLIFeatureCode(): string {
3+
// Detect if this project was created via create-cloudinary-react CLI (Integrations)
4+
function isCLI(): boolean {
55
if (typeof process !== 'undefined' && process.env) {
6-
if (process.env.CLOUDINARY_SOURCE === 'cli' || process.env.CLD_CLI === 'true') {
7-
return 'B'; // CLI feature code
8-
}
6+
return process.env.CLOUDINARY_SOURCE === 'cli' || process.env.CLD_CLI === 'true';
97
}
10-
return '0'; // Default (no specific feature)
8+
return false;
119
}
1210

11+
// When CLI: use Algorithm B with Product B (Integrations) and sdkCode H (React CLI). Otherwise React SDK: sdkCode J.
12+
const isCLIDetected = isCLI();
1313

1414
export const SDKAnalyticsConstants = {
1515
sdkSemver: 'PACKAGE_VERSION_INJECTED_DURING_BUILD',
1616
techVersion: React.version,
17-
sdkCode: 'J',
18-
feature: getCLIFeatureCode()
17+
sdkCode: isCLIDetected ? 'H' : 'J',
18+
...(isCLIDetected && { product: 'B' as const })
1919
};

0 commit comments

Comments
 (0)