Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Commit

Permalink
feat: allow multiple custom dimensions for trackEvent/trackScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
janpe committed Nov 25, 2020
1 parent 7c7c43a commit e9f75c9
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.Promise

import pro.piwik.sdk.Piwik
Expand Down Expand Up @@ -51,10 +52,10 @@ class PiwikProSdkModule(reactContext: ReactApplicationContext) : ReactContextBas
}

@ReactMethod
fun trackScreen(path: String, optionalArgs: ReadableMap, promise: Promise) {
fun trackScreen(path: String, customDimensions: ReadableArray?, promise: Promise) {
try {
var tracker = this.tracker ?: throw Exception("Tracker is not initialized")
getTrackHelperWithDimensions(optionalArgs)
getTrackHelperWithDimensions(customDimensions)
.screen(path)
.with(tracker)

Expand All @@ -65,10 +66,10 @@ class PiwikProSdkModule(reactContext: ReactApplicationContext) : ReactContextBas
}

@ReactMethod
fun trackEvent(category: String, action: String, optionalArgs: ReadableMap, promise: Promise) {
fun trackEvent(category: String, action: String, optionalArgs: ReadableMap, customDimensions: ReadableArray?, promise: Promise) {
try {
var tracker = this.tracker ?: throw Exception("Tracker is not initialized")
var track = getTrackHelperWithDimensions(optionalArgs)
var track = getTrackHelperWithDimensions(customDimensions)
.event(category, action)

if (optionalArgs.hasKey("name")) {
Expand Down Expand Up @@ -97,11 +98,20 @@ class PiwikProSdkModule(reactContext: ReactApplicationContext) : ReactContextBas
}
}

private fun getTrackHelperWithDimensions(optionalArgs: ReadableMap): TrackHelper {
private fun getTrackHelperWithDimensions(customDimensions: ReadableArray?): TrackHelper {
var trackHelper = TrackHelper.track()

if (optionalArgs.hasKey("customDimensionIndex") && optionalArgs.hasKey("customDimensionValue")) {
trackHelper.dimension(optionalArgs.getInt("customDimensionIndex"), optionalArgs.getString("customDimensionValue"))
if (customDimensions == null) {
return trackHelper
}

var iterations = customDimensions.size() - 1

for (x in 0..iterations) {
var customDimension = customDimensions.getMap(x)
if (customDimension != null) {
trackHelper.dimension(customDimension.getInt("index"), customDimension.getString("value"))
}
}

return trackHelper
Expand Down
63 changes: 36 additions & 27 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,54 +1,63 @@
import * as React from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { StyleSheet, View, Text, Button } from 'react-native';
import * as PiwikProSdk from 'react-native-piwik-pro-sdk';

export default function App() {
const [result, setResult] = React.useState<{ type: string; error?: Error }>({
type: 'Loading',
});
const [initialized, setInitialized] = React.useState(false);

const sendTestEvents = React.useCallback(async () => {
const firstCustomDimension: PiwikProSdk.CustomDimension = {
index: 1,
value: 'first',
};

const secondCustomDimension: PiwikProSdk.CustomDimension = {
index: 2,
value: 'second',
};

// Track screen view with custom dimension
await PiwikProSdk.trackScreen('main/list', [firstCustomDimension]);
// Track custom event with a custom dimension
await PiwikProSdk.trackEvent('app', 'launch', 'notification', 1.04, [
firstCustomDimension,
]);
// Track custom event with multiple custom dimensions
await PiwikProSdk.trackEvent('app', 'test1', undefined, undefined, [
firstCustomDimension,
secondCustomDimension,
]);
// Track custom event withouth custom dimensions
await PiwikProSdk.trackEvent('app', 'test3');
// Immediately dispatch all events
await PiwikProSdk.dispatch();
}, []);

React.useEffect(() => {
(async () => {
if (initialized) {
return;
}
// Initialize the SDK
await PiwikProSdk.init(
'https://demoaccess.piwik.pro/',
'3e7e6ab9-d605-42b0-ac1b-cdf5bb5e216f'
);

// Track screen view with custom dimension
await PiwikProSdk.trackScreen('main/list', 1, 'first');
// Track custom events with custom dimensions
await PiwikProSdk.trackEvent(
'app',
'launch',
'notification',
1.04,
1,
'first'
);
await PiwikProSdk.trackEvent(
'app',
'test1',
undefined,
undefined,
2,
'second'
);
// Track custom event withouth custom dimensions
await PiwikProSdk.trackEvent('app', 'test3');
// Immediately dispatch all events
await PiwikProSdk.dispatch();
setInitialized(true);
})().then(
() => setResult({ type: 'Success' }),
(error) => setResult({ type: 'Error', error })
);
}, []);
}, [initialized]);

return (
<View style={styles.container}>
<Text>Sending test events</Text>
<Text>Status: {result.type}</Text>
{result.error && <Text>{result.error.message}</Text>}
<Button title="Send test events" onPress={sendTestEvents} />
</View>
);
}
Expand Down
13 changes: 7 additions & 6 deletions ios/PiwikProSdk.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ @implementation PiwikProSdk

RCT_REMAP_METHOD(trackScreen,
trackScreenWithPath:(nonnull NSString*)path
optionalArgs:(nonnull NSDictionary*)optionalArgs
customDimensions:(NSArray*)customDimensions
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
{
@try {
if ([optionalArgs[@"customDimensionIndex"] isKindOfClass:[NSNumber class]] && optionalArgs[@"customDimensionValue"]) {
[self setDimension:optionalArgs[@"customDimensionIndex"] value:optionalArgs[@"customDimensionValue"]];
for (NSDictionary* customDimension in customDimensions) {
[self setDimension:customDimension[@"index"] value:customDimension[@"value"]];
}

[[PiwikTracker sharedInstance] sendView:path];
Expand All @@ -68,13 +68,14 @@ @implementation PiwikProSdk
trackScreenWithCategory:(nonnull NSString*)category
withAction:(nonnull NSString*)action
optionalArgs:(nonnull NSDictionary*)optionalArgs
customDimensions:(NSArray*)customDimensions
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
{
@try {
if ([optionalArgs[@"customDimensionIndex"] isKindOfClass:[NSNumber class]] && optionalArgs[@"customDimensionValue"]) {
[self setDimension:optionalArgs[@"customDimensionIndex"] value:optionalArgs[@"customDimensionValue"]];
}
for (NSDictionary* customDimension in customDimensions) {
[self setDimension:customDimension[@"index"] value:customDimension[@"value"]];
}

[[PiwikTracker sharedInstance]
sendEventWithCategory:category
Expand Down
5 changes: 2 additions & 3 deletions src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ describe('react-native-piwik-pro-sdk', () => {
{
name: undefined,
value: undefined,
customDimensionIndex: undefined,
customDimensionValue: undefined,
}
},
undefined
);
});
});
Expand Down
43 changes: 20 additions & 23 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ type TrackerOptions = Partial<{
isPrefixingEnabled: boolean;
}>;

export interface CustomDimension {
index: number;
value: string;
}

type PiwikProSdkType = {
init(baseUrl: string, siteId: string, options: TrackerOptions): Promise<void>;
trackScreen(
path: string,
// Optional arguments need to be passed in map
// since nullable numbers are not supported
optionalArgs: {
customDimensionIndex?: number;
customDimensionValue?: string;
}
customDimensions?: CustomDimension[]
): Promise<void>;
trackEvent(
category: string,
Expand All @@ -45,9 +45,8 @@ type PiwikProSdkType = {
optionalArgs: {
name?: string;
value?: number;
customDimensionIndex?: number;
customDimensionValue?: string;
}
},
customDimensions?: CustomDimension[]
): Promise<void>;
dispatch(): Promise<void>;
};
Expand Down Expand Up @@ -79,13 +78,9 @@ export async function init(
*/
export async function trackScreen(
path: string,
customDimensionIndex?: number,
customDimensionValue?: string
customDimensions?: CustomDimension[]
): Promise<void> {
return await PiwikProSdk.trackScreen(path, {
customDimensionIndex,
customDimensionValue,
});
return await PiwikProSdk.trackScreen(path, customDimensions);
}

/**
Expand All @@ -100,15 +95,17 @@ export async function trackEvent(
action: string,
name?: string,
value?: number,
customDimensionIndex?: number,
customDimensionValue?: string
customDimensions?: CustomDimension[]
): Promise<void> {
return await PiwikProSdk.trackEvent(category, action, {
name,
value,
customDimensionIndex,
customDimensionValue,
});
return await PiwikProSdk.trackEvent(
category,
action,
{
name,
value,
},
customDimensions
);
}

/**
Expand Down

0 comments on commit e9f75c9

Please sign in to comment.