Skip to content

Commit

Permalink
fix: send event props as third param in track call
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhi591 authored and rohitesh-wingify committed Jun 7, 2024
1 parent 5c85c97 commit 3466108
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

[1.1.0] - 2024-06-07

### Fixed
Send event properties as third param in trackEvent() call

[1.0.0] - 2024-05-31

### Added
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ limitations under the License. -->

<groupId>com.vwo.sdk</groupId>
<artifactId>vwo-fme-java-sdk</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>VWO FME Java SDK</name>
Expand Down
33 changes: 31 additions & 2 deletions src/main/java/com/vwo/VWOClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.vwo.services.LoggerService;
import com.vwo.services.UrlService;
import com.vwo.utils.DataTypeUtil;
import com.vwo.utils.SDKMetaUtil;
import com.vwo.utils.SettingsUtil;

import java.util.HashMap;
Expand All @@ -54,7 +55,10 @@ public VWOClient(String settings, VWOInitOptions options) {
this.settings = settings;
this.processedSettings = objectMapper.readValue(settings, Settings.class);
SettingsUtil.processSettings(this.processedSettings);
// init url version with collection prefix
UrlService.init(this.processedSettings.getCollectionPrefix());
// init SDKMetaUtil and set sdkVersion
SDKMetaUtil.init();
LoggerService.log(LogLevelEnum.INFO, "CLIENT_INITIALIZED", null);
} catch (Exception exception) {
LoggerService.log(LogLevelEnum.ERROR, "exception occurred while parsing settings " + exception.getMessage());
Expand Down Expand Up @@ -119,9 +123,10 @@ public GetFlag getFlag(String featureKey, VWOContext context) {
* This method is used to track the event
* @param eventName Event name to be tracked
* @param context User context
* @param eventProperties event properties to be sent for the event
* @return Map containing the event name and its status
*/
public Map<String, Boolean> trackEvent(String eventName, VWOContext context) {
private Map<String, Boolean> track(String eventName, VWOContext context, Map<String, ?> eventProperties) {
String apiName = "trackEvent";
Map<String, Boolean> resultMap = new HashMap<>();
try {
Expand Down Expand Up @@ -149,7 +154,7 @@ public Map<String, Boolean> trackEvent(String eventName, VWOContext context) {
return resultMap;
}

Boolean result = TrackEventAPI.track(this.processedSettings, eventName, context, hooksManager);
Boolean result = TrackEventAPI.track(this.processedSettings, eventName, context, eventProperties, hooksManager);
if (result) {
resultMap.put(eventName, true);
} else {
Expand All @@ -166,6 +171,30 @@ public Map<String, Boolean> trackEvent(String eventName, VWOContext context) {
}
}

/**
* Overloaded function if event properties need to be passed
* calls track method to track the event
* @param eventName Event name to be tracked
* @param context User context
* @param eventProperties event properties to be sent for the event
* @return Map containing the event name and its status
*/
public Map<String, Boolean> trackEvent(String eventName, VWOContext context, Map<String, ?> eventProperties) {
return track(eventName, context, eventProperties);
}

/**
* Overloaded function for no event properties
* calls track method to track the event
* @param eventName Event name to be tracked
* @param context User context
* @return Map containing the event name and its status
*/
public Map<String, Boolean> trackEvent(String eventName, VWOContext context) {
return track(eventName, context, new HashMap<>());
}


/**
* Sets an attribute for a user in the context provided.
* This method validates the types of the inputs before proceeding with the API call.
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/vwo/api/TrackEventAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ public class TrackEventAPI {
* @param settings The settings model containing configuration.
* @param eventName The name of the event to track.
* @param context The user context model containing user-specific data.
* @param eventProperties event properties for the event
* @param hooksManager The hooks manager instance.
* @return Boolean indicating if the event was successfully tracked.
*/
public static Boolean track(Settings settings, String eventName, VWOContext context, HooksManager hooksManager) {
public static Boolean track(Settings settings, String eventName, VWOContext context, Map<String, ?> eventProperties, HooksManager hooksManager) {
try {
if (FunctionUtil.doesEventBelongToAnyFeature(eventName, settings)) {
createAndSendImpressionForTrack(settings, eventName, context);
createAndSendImpressionForTrack(settings, eventName, context, eventProperties);
Map<String, Object> objectToReturn = new HashMap<>();
objectToReturn.put("eventName", eventName);
objectToReturn.put("api", ApiEnum.TRACK.getValue());
Expand Down Expand Up @@ -71,11 +72,13 @@ public static Boolean track(Settings settings, String eventName, VWOContext cont
* @param settings The settings model containing configuration.
* @param eventName The name of the event to track.
* @param context The user context model containing user-specific data.
* @param eventProperties event properties for the event
*/
private static void createAndSendImpressionForTrack(
Settings settings,
String eventName,
VWOContext context
VWOContext context,
Map<String, ?> eventProperties
) {
// Get base properties for the event
Map<String, String> properties = NetworkUtil.getEventsBaseProperties(
Expand All @@ -90,7 +93,8 @@ private static void createAndSendImpressionForTrack(
settings,
context.getId(),
eventName,
context
context,
eventProperties
);

// Send the constructed properties and payload as a POST request
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/vwo/constants/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class Constants {
public static final long DEFAULT_REQUEST_TIME_INTERVAL = 600; // 10 * 60(secs) = 600 secs i.e. 10 minutes
public static final int DEFAULT_EVENTS_PER_REQUEST = 100;
public static final String SDK_NAME = "vwo-fme-java-sdk";
public static final String SDK_VERSION = "1.0.0";
public static final long SETTINGS_EXPIRY = 10000000;
public static final long SETTINGS_TIMEOUT = 50000;

Expand Down
9 changes: 0 additions & 9 deletions src/main/java/com/vwo/models/user/VWOContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class VWOContext {
private Map<String, ?> customVariables = new HashMap<>();

private Map<String, ?> variationTargetingVariables = new HashMap<>();
private Map<String, ?> eventProperties = new HashMap<>();

private GatewayService _vwo;

Expand Down Expand Up @@ -76,12 +75,4 @@ public GatewayService getVwo() {
public void setVwo(GatewayService _vwo) {
this._vwo = _vwo;
}

public Map<String, ?> getEventProperties() {
return eventProperties;
}

public void setEventProperties(Map<String, ?> eventProperties) {
this.eventProperties = eventProperties;
}
}
7 changes: 4 additions & 3 deletions src/main/java/com/vwo/utils/NetworkUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private static Event createEvent(String eventName, Settings settings) {
private static Props createProps(Settings settings) {
Props props = new Props();
props.setSdkName(Constants.SDK_NAME);
props.setSdkVersion(Constants.SDK_VERSION);
props.setSdkVersion(SDKMetaUtil.getSdkVersion());
props.setEnvKey(settings.getSdkKey());
return props;
}
Expand Down Expand Up @@ -191,12 +191,13 @@ public static Map<String, Object> getTrackUserPayloadData(Settings settings, Str
* @param userId The ID of the user.
* @param eventName The name of the event.
* @param context The user context model containing user-specific data.
* @param eventProperties event properties for the event
* @return Map containing the payload data.
*/
public static Map<String, Object> getTrackGoalPayloadData(Settings settings, String userId, String eventName, VWOContext context) {
public static Map<String, Object> getTrackGoalPayloadData(Settings settings, String userId, String eventName, VWOContext context, Map<String, ?> eventProperties) {
EventArchPayload properties = getEventBasePayload(settings, userId, eventName, context.getUserAgent(), context.getIpAddress());
properties.getD().getEvent().getProps().setIsCustomEvent(true);
addCustomEventProperties(properties, (Map<String, Object>) context.getEventProperties());
addCustomEventProperties(properties, (Map<String, Object>) eventProperties);
LoggerService.log(LogLevelEnum.DEBUG, "IMPRESSION_FOR_TRACK_GOAL", new HashMap<String, String>() {
{
put("eventName", eventName);
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/com/vwo/utils/SDKMetaUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright 2024 Wingify Software Pvt. Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.vwo.utils;

import com.vwo.services.UrlService;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

public class SDKMetaUtil {

private static final String POM_FILE_PATH = "pom.xml";
private static String sdkVersion;

/**
* Initializes the SDKMetaUtil with the sdkVersion from pom.xml
*/
public static void init() {
try {
File pomFile = new File(POM_FILE_PATH);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(pomFile);
doc.getDocumentElement().normalize();
Element versionElement = (Element) doc.getElementsByTagName("version").item(0);
sdkVersion = versionElement.getTextContent();
} catch (Exception e) {
sdkVersion = "1.0.0-error";
throw new RuntimeException("Failed to read version from pom.xml", e);
}
}

/**
* Returns the sdkVersion
*/
public static String getSdkVersion(){
return sdkVersion;
}
}

0 comments on commit 3466108

Please sign in to comment.