Skip to content

Commit

Permalink
Add analytics token
Browse files Browse the repository at this point in the history
  • Loading branch information
adimiz1 committed Jun 12, 2023
1 parent 8f81e9f commit df66dd9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/AdvancedImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { Image, ImageProps, View} from 'react-native';
import type { CloudinaryImage } from '@cloudinary/url-gen';
import 'react-native-url-polyfill/auto';
import { SDKAnalyticsConstants } from './internal/SDKAnalyticsConstants';

interface AdvancedImageProps extends Omit<ImageProps, 'source'> { cldImg: CloudinaryImage; }
const AdvancedImage: React.FC<AdvancedImageProps> = (props) => {
Expand All @@ -11,7 +12,7 @@ const AdvancedImage: React.FC<AdvancedImageProps> = (props) => {
} = props;
return (
<View>
<Image {...rest} source={{ uri: cldImg.toURL() }} />
<Image {...rest} source={{ uri: cldImg.toURL({trackedAnalytics: SDKAnalyticsConstants}) }} />
</View>
);
}
Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/analytics.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { SDKAnalyticsConstants } from '../internal/SDKAnalyticsConstants';
import { Platform, Image } from 'react-native';
import AdvancedImage from '../AdvancedImage';
import { CloudinaryImage } from '@cloudinary/url-gen/assets/CloudinaryImage';
import { render } from '@testing-library/react-native';
import React from 'react';

const cloudinaryImage = new CloudinaryImage('sample', { cloudName: 'demo' });

describe('analytics', () => {
beforeEach(() => {
SDKAnalyticsConstants.sdkSemver = '1.0.0';
SDKAnalyticsConstants.techVersion = '10.2.5';
});
it('creates a url with analytics', () => {
const element = render(<AdvancedImage cldImg={cloudinaryImage}></AdvancedImage>);
const imageComponent = element.root.findByType(Image);
expect(imageComponent.props.source.uri).toBe(cloudinaryImage.toURL({trackedAnalytics: SDKAnalyticsConstants}));
});
});
4 changes: 2 additions & 2 deletions src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { render } from '@testing-library/react-native';
import TestRenderer from 'react-test-renderer';
describe('AdvancedImage', () => {
it('should render an Image with the correct URI', () => {
const cldImg = new CloudinaryImage('sample', { cloudName: 'demo' });
const cldImg = new CloudinaryImage('sample', { cloudName: 'demo' }, { analytics: false });
const component = TestRenderer.create(<AdvancedImage cldImg={cldImg} />);
const imageComponent = component.root.findByType(Image);
expect(imageComponent.props.source.uri).toBe(cldImg.toURL());
});

it('should forward any other props to the Image', () => {
const cldImg = new CloudinaryImage('sample', { cloudName: 'demo' });
const cldImg = new CloudinaryImage('sample', { cloudName: 'demo'} , { analytics: false });
const { getByTestId } = render(<AdvancedImage cldImg={cldImg} testID="my-image" />);
const image = getByTestId('my-image');
expect(image).toBeTruthy();
Expand Down
29 changes: 29 additions & 0 deletions src/internal/SDKAnalyticsConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Platform } from 'react-native';

const getReactNativeVersion = () => {
try{
const version = Platform.Version;
return version.toString();
} catch {
return "0.0.0"
}
}

const getSDKVersion = () => {
try{
const SDKVersionPackageJson = require('cloudinary-react-native/package.json')
if (SDKVersionPackageJson && SDKVersionPackageJson.version) {
//return SDKVersionPackageJson.version
return
}
} catch {
return "0.0.0";
}
return "0.0.0";
}

export const SDKAnalyticsConstants = {
sdkSemver: getSDKVersion(),
techVersion: getReactNativeVersion(),
sdkCode: 'P'
};

0 comments on commit df66dd9

Please sign in to comment.