Skip to content

Commit

Permalink
interim checkin for JavaScript SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
dipanjanb authored and sayan-rudder committed Sep 30, 2019
1 parent 77990d2 commit 992d1ed
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 14 deletions.
77 changes: 64 additions & 13 deletions rudder-client-javascript/RudderAnalyticsClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ function generateUUID() { // Public Domain/MIT
});
}

//Event Type enumeration
var EventType = {
//Message Type enumeration
var MessageType = {
TRACK: "track",
PAGE: "page",
SCREEN: "screen",
Expand Down Expand Up @@ -79,9 +79,9 @@ var Analytics = (function () {
//Track function
track: function(event){
if(event.message){ //process only if valid message is there
event.message.validateFor(EventType.TRACK);
event.message.validateFor(MessageType.TRACK);
//validated, now set event type and add to flush queue
event.message.type = EventType.TRACK.value;
event.message.type = MessageType.TRACK.value;
addToFlushQueue(event);
}

Expand All @@ -104,7 +104,6 @@ var Analytics = (function () {
throw new Error("Application context cannot be null");
}

console.log(JSON.stringify(context));
instance = init();

//Initialize
Expand All @@ -121,6 +120,15 @@ var Analytics = (function () {

})();

//Message payload class
class RudderElement {
constructor(){
this.rl_message = new RudderMessage();

}
}


//Core message class with default values
class RudderMessage {

Expand Down Expand Up @@ -149,27 +157,62 @@ class RudderMessage {
this.rl_properties.set(key, value);
}

//Validate whether this message is semantically valid for the
//event type mentioned
validateFor (eventType){
//Validate whether this message is semantically valid for the type mentioned
validateFor (messageType){
//First check that rl_properties is populated
if(!this.rl_properties){
throw new Error("Key rl_properties is required");
}
//Event type specific checks
switch (eventType){
case EventType.TRACK:
switch (messageType){
case MessageType.TRACK:
//check if rl_event is present
if (!this.rl_event){
throw new Error("Key rl_event is required for track event");
}
//Next make specific checks for e-commerce events
if (this.rl_event in Object.values(ECommerceEvents)){
switch(rl_event){
case ECommerceEvents.CHECKOUT_STEP_VIEWED:
case ECommerceEvents.CHECKOUT_STEP_COMPLETED:
case ECommerceEvents.PAYMENT_INFO_ENTERED:
checkForKey("checkout_id");
checkForKey("step");
break;
case ECommerceEvents.PROMOTION_VIEWED:
case ECommerceEvents.PROMOTION_CLICKED:
checkForKey("promotion_id");
break;
case ECommerceEvents.ORDER_REFUNDED:
checkForKey("order_id");
break;
default:
}
} else if (!this.rl_properties.has("category") ||
!this.rl_properties.get("category")){
throw new Error("Key category is required in rl_properties");
}

break;
case EventType.PAGE:
case MessageType.PAGE:
break;
case MessageType.SCREEN:
if (!this.rl_properties.has("name") ||
!this.rl_properties.get("name")){
throw new Error("Key name is required in rl_properties");
}
break;

}

}

//Function for checking existence of a particular property
checkForKey(propertyName){
if(!this.rl_properties.has(propertyName) ||
!this.rl_properties.get(propertyName)) {
throw new Error("Key " + propertyName + " is required in rl_properties");
}
}
}

//Context class
Expand Down Expand Up @@ -294,10 +337,18 @@ class RudderNetwork {
this.rl_carrier = "";
}
}


//Rudder event class
class RudderEvent {

}

//Test code
context = new RudderContext();
context.applicationContext = {};
var Instance1 = Analytics.getInstance(context);
console.log(JSON.stringify(new RudderMessage()));
console.log(JSON.stringify(new RudderElement()));



2 changes: 1 addition & 1 deletion rudder-client-javascript/RudderAnalyticsClient.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 992d1ed

Please sign in to comment.