Skip to content

Commit c6bd741

Browse files
authored
feat: Fixes inability to turn off URL analytics (#101)
# Description Updates the construct URL function to work properly when analytics is disabled. Previous, passing the config.url.analytics as false would work, but not the analytics function option. This normalizes behavior and makes sure it works as expected ## Issue Ticket Number Helps to address issue found: nuxt-modules/cloudinary#174 <!-- Specify above which issue this fixes by referencing the issue number (`#<ISSUE_NUMBER>`) or issue URL. --> <!-- Example: Fixes https://github.com/colbyfayock/cloudinary-util/issues/<ISSUE_NUMBER> --> ## Type of change <!-- Please select all options that are applicable. --> - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Fix or improve the documentation - [ ] This change requires a documentation update # Checklist <!-- These must all be followed and checked. --> - [ ] I have followed the contributing guidelines of this project as mentioned in [CONTRIBUTING.md](/CONTRIBUTING.md) - [ ] I have created an [issue](https://github.com/colbyfayock/cloudinary-util/issues) ticket for this PR - [ ] I have checked to ensure there aren't other open [Pull Requests](https://github.com/colbyfayock/cloudinary-util/pulls) for the same update/change? - [ ] I have performed a self-review of my own code - [ ] I have run tests locally to ensure they all pass - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes needed to the documentation
1 parent 1b0d6c2 commit c6bd741

2 files changed

Lines changed: 53 additions & 9 deletions

File tree

packages/url-loader/src/lib/cloudinary.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const transformationPlugins = [
6666
export interface ConstructUrlProps {
6767
options: ImageOptions;
6868
config?: ConfigOptions;
69-
analytics?: AnalyticsOptions;
69+
analytics?: AnalyticsOptions | false;
7070
}
7171

7272
export interface PluginOptionsResize {
@@ -83,7 +83,17 @@ export interface PluginResults {
8383
options?: PluginOptions;
8484
}
8585

86-
export function constructCloudinaryUrl({ options, config, analytics }: ConstructUrlProps): string {
86+
export function constructCloudinaryUrl({ options, config = {}, analytics }: ConstructUrlProps): string {
87+
// If someone is explicitly passing in undefined for analytics via the analytics option,
88+
// ensure that the URL Gen SDK option is being passed in as false as well
89+
90+
if ( analytics === false ) {
91+
if ( typeof config?.url === 'undefined' ) {
92+
config.url = {};
93+
}
94+
config.url.analytics = false;
95+
}
96+
8797
const cld = new Cloudinary(config);
8898

8999
if ( typeof options?.src !== 'string' ) {

packages/url-loader/tests/lib/cloudinary.spec.js

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -503,13 +503,7 @@ describe('Cloudinary', () => {
503503
expect(url).toContain(`?_a=${expectedId}`);
504504
});
505505

506-
});
507-
508-
/* Custom Config */
509-
510-
describe('analytics', () => {
511-
512-
it('should include an analytics ID at the end of the URL', () => {
506+
it('should include an analytics ID at the end of the URL when using a custom config', () => {
513507
const cloudName = 'customtestcloud';
514508
const secureDistribution = 'spacejelly.dev';
515509
const url = constructCloudinaryUrl({
@@ -528,6 +522,46 @@ describe('Cloudinary', () => {
528522
expect(url).toContain(`https://${secureDistribution}/${cloudName}`);
529523
});
530524

525+
it('should not include an analytics ID with config.url.analytics set to false', () => {
526+
const cloudName = 'customtestcloud';
527+
const url = constructCloudinaryUrl({
528+
options: {
529+
src: 'turtle',
530+
},
531+
config: {
532+
cloud: {
533+
cloudName
534+
},
535+
url: {
536+
analytics: false
537+
}
538+
},
539+
analytics: {
540+
sdkCode: 'A',
541+
sdkSemver: '1.0.0',
542+
techVersion: '1.2.3',
543+
product: 'B'
544+
}
545+
});
546+
expect(url).not.toContain(`?_a`);
547+
});
548+
549+
it('should not include an analytics ID with analytics set to false', () => {
550+
const cloudName = 'customtestcloud';
551+
const url = constructCloudinaryUrl({
552+
options: {
553+
src: 'turtle',
554+
},
555+
config: {
556+
cloud: {
557+
cloudName
558+
}
559+
},
560+
analytics: false
561+
});
562+
expect(url).not.toContain(`?_a`);
563+
});
564+
531565
});
532566

533567
/* Raw Transformations */

0 commit comments

Comments
 (0)