Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create event interface #7

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ dependencies {
// For < 0.71, this will be from the local maven repo
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-android:0.73.1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep a pinned version

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yah or maybe a range of versions?

implementation "com.facebook.react:react-android:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "com.github.klaviyo.klaviyo-android-sdk:analytics:1.3.5"
implementation "com.github.klaviyo.klaviyo-android-sdk:push-fcm:1.3.5"
Expand Down
2 changes: 1 addition & 1 deletion android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx1024m -XX:MaxPermSize=256m
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
#
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.klaviyoreactnativesdk

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReadableMap
import com.klaviyo.analytics.Klaviyo
import com.klaviyo.analytics.model.*
Expand Down Expand Up @@ -33,21 +32,14 @@ class KlaviyoReactNativeSdkModule internal constructor(private val context: Reac
}

@ReactMethod
override fun createEvent(event: ReadableMap) {
val parsedEvent = event.toHashMap()
val eventName = parsedEvent.remove("event") as HashMap<String, String>
val eventProperties = parsedEvent.map { entry ->
EventKey.CUSTOM(entry.key) as EventKey to entry.value as Serializable
}.toMap()

println(eventName["name"])
println(eventProperties)

override fun createEvent(name: String, properties: ReadableMap?) {
Klaviyo.initialize("LuYLmF", context)
Klaviyo.createEvent(Event(
type = eventName["name"] as String,
properties = eventProperties
))
val event = Event(
type = name,
properties =properties?.toHashMap()?.map { entry -> EventKey.CUSTOM(entry.key) as EventKey to entry.value as Serializable }
?.toMap(),
)
Klaviyo.createEvent(event = event)
}

companion object {
Expand Down
2 changes: 1 addition & 1 deletion android/src/oldarch/KlaviyoReactNativeSdkSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import com.facebook.react.bridge.ReadableMap
abstract class KlaviyoReactNativeSdkSpec internal constructor(context: ReactApplicationContext) :
ReactContextBaseJavaModule(context) {

abstract fun createEvent(event: ReadableMap)
abstract fun createEvent(name: String, properties: ReadableMap?)
}
15 changes: 5 additions & 10 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ export default function App() {
const [result] = React.useState<number | undefined>();

React.useEffect(() => {
Klaviyo.createEvent({
event: EventType.CUSTOM('EXAMPLE_EVENT'),
properties: {
[EventProperty.EVENT_ID.name]: '123',
},
Klaviyo.createEvent(EventType.OPENED_PUSH, {
[EventProperty.EVENT_ID]: '123',
});
}, []);

Expand All @@ -21,11 +18,9 @@ export default function App() {
<Button
title="Click Me"
onPress={() => {
Klaviyo.createEvent({
event: EventType.CUSTOM('EXAMPLE_EVENT_2'),
properties: {
[EventProperty.EVENT_ID.name]: '321',
},
Klaviyo.createCustomEvent('CUSTOM_EVENT', {
custom_property_2: '321',
[EventProperty.VALUE]: 12,
});
}}
/>
Expand Down
38 changes: 16 additions & 22 deletions src/Event.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
export class EventType {
public name: string;

private constructor(name: string) {
this.name = name;
}

public static OPENED_PUSH: EventType = new EventType('$opened_push');
public static CUSTOM = (name: string): EventType => new EventType(name);
export enum EventType {
OPENED_PUSH = '$opened_push',
}

export class EventProperty {
public name: string;

private constructor(name: string) {
this.name = name;
}

public static EVENT_ID: EventProperty = new EventProperty('$event_id');
public static CUSTOM = (name: string): EventProperty =>
new EventProperty(name);
export enum EventProperty {
EVENT_ID = '$event_id',
VALUE = 'value',
}

export interface IEvent {
event: EventType;
properties?: Object;
type KlaviyoEventPropertyType = EventProperty | string;

export interface KlaviyoEventAPI {
readonly createEvent: (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

name: EventType,
properties?: Record<KlaviyoEventPropertyType, Object>
) => void;
readonly createCustomEvent: (
name: string,
properties?: Record<KlaviyoEventPropertyType, Object>
) => void;
}
10 changes: 5 additions & 5 deletions src/NativeKlaviyoReactNativeSdk.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';
import type { IEvent } from './Event';
import type { KlaviyoEventAPI } from './Event';

export interface Spec extends TurboModule {
createEvent(event: IEvent): void;
}
export interface KlaviyoSpec extends TurboModule, KlaviyoEventAPI {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@evan-masseau evan-masseau Dec 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may cause a turbo modules issue. I was about to suggest we extract this interface to its own file but I happened on this first which explains some naming conventions involved in turbo modules code gen. It mentions the filename, but doesn't say whether this interface has to be called Spec.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yea here we go https://dev.to/amazonappdev/a-guide-to-turbo-modules-in-react-native-5aa3

This interface type must be named Spec for a Turbo Module.

Maybe lets leave this file as-is from the code generator, and have a IKlaviyo in a separate file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i mean sure the naming of the file can be Spec if thats required but i dont see a point in defining another interface that mirrors this spec interface


export default TurboModuleRegistry.getEnforcing<Spec>('KlaviyoReactNativeSdk');
export default TurboModuleRegistry.getEnforcing<KlaviyoSpec>(
'KlaviyoReactNativeSdk'
);
16 changes: 8 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { IEvent } from './Event';
import { KlaviyoReactNativeSdk } from './KlaviyoReactNativeSdk';
import { EventType } from './Event';
import type { KlaviyoSpec } from './NativeKlaviyoReactNativeSdk';

interface IKlaviyo {
readonly createEvent: (event: IEvent) => void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll wind up with IKlaviyo again for initialize, but we can add it back

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can just implement Spec instead if thats the case, as long as its a single interface

}

export const Klaviyo: IKlaviyo = {
createEvent(event: IEvent): void {
KlaviyoReactNativeSdk.createEvent(event);
export const Klaviyo: KlaviyoSpec = {
createEvent(name: EventType, properties?: Record<any, Object>): void {
this.createCustomEvent(name, properties);
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this need .toString() ?

createCustomEvent(name: string, properties?: Record<any, Object>): void {
KlaviyoReactNativeSdk.createEvent(name, properties);
},
};

Expand Down
Loading