Skip to content

Commit df66dd9

Browse files
authored
Add analytics token
1 parent 8f81e9f commit df66dd9

4 files changed

Lines changed: 53 additions & 3 deletions

File tree

src/AdvancedImage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import { Image, ImageProps, View} from 'react-native';
33
import type { CloudinaryImage } from '@cloudinary/url-gen';
44
import 'react-native-url-polyfill/auto';
5+
import { SDKAnalyticsConstants } from './internal/SDKAnalyticsConstants';
56

67
interface AdvancedImageProps extends Omit<ImageProps, 'source'> { cldImg: CloudinaryImage; }
78
const AdvancedImage: React.FC<AdvancedImageProps> = (props) => {
@@ -11,7 +12,7 @@ const AdvancedImage: React.FC<AdvancedImageProps> = (props) => {
1112
} = props;
1213
return (
1314
<View>
14-
<Image {...rest} source={{ uri: cldImg.toURL() }} />
15+
<Image {...rest} source={{ uri: cldImg.toURL({trackedAnalytics: SDKAnalyticsConstants}) }} />
1516
</View>
1617
);
1718
}

src/__tests__/analytics.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { SDKAnalyticsConstants } from '../internal/SDKAnalyticsConstants';
2+
import { Platform, Image } from 'react-native';
3+
import AdvancedImage from '../AdvancedImage';
4+
import { CloudinaryImage } from '@cloudinary/url-gen/assets/CloudinaryImage';
5+
import { render } from '@testing-library/react-native';
6+
import React from 'react';
7+
8+
const cloudinaryImage = new CloudinaryImage('sample', { cloudName: 'demo' });
9+
10+
describe('analytics', () => {
11+
beforeEach(() => {
12+
SDKAnalyticsConstants.sdkSemver = '1.0.0';
13+
SDKAnalyticsConstants.techVersion = '10.2.5';
14+
});
15+
it('creates a url with analytics', () => {
16+
const element = render(<AdvancedImage cldImg={cloudinaryImage}></AdvancedImage>);
17+
const imageComponent = element.root.findByType(Image);
18+
expect(imageComponent.props.source.uri).toBe(cloudinaryImage.toURL({trackedAnalytics: SDKAnalyticsConstants}));
19+
});
20+
});

src/__tests__/index.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import { render } from '@testing-library/react-native';
66
import TestRenderer from 'react-test-renderer';
77
describe('AdvancedImage', () => {
88
it('should render an Image with the correct URI', () => {
9-
const cldImg = new CloudinaryImage('sample', { cloudName: 'demo' });
9+
const cldImg = new CloudinaryImage('sample', { cloudName: 'demo' }, { analytics: false });
1010
const component = TestRenderer.create(<AdvancedImage cldImg={cldImg} />);
1111
const imageComponent = component.root.findByType(Image);
1212
expect(imageComponent.props.source.uri).toBe(cldImg.toURL());
1313
});
1414

1515
it('should forward any other props to the Image', () => {
16-
const cldImg = new CloudinaryImage('sample', { cloudName: 'demo' });
16+
const cldImg = new CloudinaryImage('sample', { cloudName: 'demo'} , { analytics: false });
1717
const { getByTestId } = render(<AdvancedImage cldImg={cldImg} testID="my-image" />);
1818
const image = getByTestId('my-image');
1919
expect(image).toBeTruthy();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Platform } from 'react-native';
2+
3+
const getReactNativeVersion = () => {
4+
try{
5+
const version = Platform.Version;
6+
return version.toString();
7+
} catch {
8+
return "0.0.0"
9+
}
10+
}
11+
12+
const getSDKVersion = () => {
13+
try{
14+
const SDKVersionPackageJson = require('cloudinary-react-native/package.json')
15+
if (SDKVersionPackageJson && SDKVersionPackageJson.version) {
16+
//return SDKVersionPackageJson.version
17+
return
18+
}
19+
} catch {
20+
return "0.0.0";
21+
}
22+
return "0.0.0";
23+
}
24+
25+
export const SDKAnalyticsConstants = {
26+
sdkSemver: getSDKVersion(),
27+
techVersion: getReactNativeVersion(),
28+
sdkCode: 'P'
29+
};

0 commit comments

Comments
 (0)