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

complete npm package #50

Merged
merged 11 commits into from
Mar 25, 2024
Merged
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
3 changes: 2 additions & 1 deletion api/src/controllers/events/input-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ const inputEventRoute = APIWrapper({
environment
}

await createInputEvent(event);
const createdEvent = await createInputEvent(event);
return createdEvent;
},
},
GET: {
Expand Down
3 changes: 2 additions & 1 deletion api/src/controllers/events/visit-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const visitEventRoute = APIWrapper({
environment
}

await createVisitEvent(event);
const createdEvent = await createVisitEvent(event);
return createdEvent;
},
},
GET: {
Expand Down
8 changes: 5 additions & 3 deletions api/src/controllers/graphs/custom-graph-type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createCustomGraphType, getCustomGraphTypes, deleteCustomGraphType } from "@/src/actions/custom-graph-type";
import { getProjectByClientKey } from "@/src/actions/project";
import { getProjectByClientKey, getProjectIDByName } from "@/src/actions/project";
import { relogRequestHandler } from "@/src/middleware/request-middleware";
import APIWrapper from "@/src/utils/api-wrapper";
import { CustomGraphType } from "@/src/utils/types";
Expand Down Expand Up @@ -59,11 +59,13 @@ const customGraphTypeRoute = APIWrapper({
requireServerToken: false
},
handler: async (req: Request) => {
const { projectId, eventTypeId } = req.body
const { projectName, eventTypeId } = req.query;
const projectId = await getProjectIDByName(projectName as string);

if (!projectId || !eventTypeId) {
throw new Error("You must specify a project and event type to get custom event types!")
}
let graphTypes = await getCustomGraphTypes(eventTypeId, projectId);
let graphTypes = await getCustomGraphTypes(eventTypeId as string, projectId as string);
return graphTypes;
},
},
Expand Down
10 changes: 10 additions & 0 deletions docker-compose-package-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3'
services:
package-test:
network_mode: "host"
build:
context: .
dockerfile: package/Dockerfile.dev
volumes:
- ./package:/app
- /app/node_modules
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ services:
- bog-analytics-net
volumes:
- mongodb-data:/data/db

networks:
bog-analytics-net:
driver: bridge
Expand Down
19 changes: 19 additions & 0 deletions package/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Use an official Node.js runtime as the base image
FROM node:18.17.0

# Set the working directory inside the container
WORKDIR /app

# Copy package.json and yarn.lock to the working directory
COPY package/package.json package/yarn.lock ./

# Install dependencies
RUN yarn install

# Copy the rest of the application code to the container
COPY package ./

RUN chmod -R 777 node_modules/

# Start the web app using yarn dev with hot reloading
ENTRYPOINT ["yarn", "test"]
59 changes: 52 additions & 7 deletions package/__tests__/analytics-logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,59 @@
import { beforeAll, describe, test } from '@jest/globals';
import { beforeAll, describe, expect, test } from '@jest/globals';
import { AnalyticsLogger } from '@/index';

import { externalRequest } from '@/utils/requests';
import { urls } from '@/utils/urls';
import { ClickEventProperties, EventEnvironment, HttpMethod, InputEventProperties, Project, VisitEventProperties } from '@/utils/types';
import { randomUUID } from "crypto";

describe('Analytics Logger Module', () => {
let logger: AnalyticsLogger;
beforeAll(() => {
logger = new AnalyticsLogger()
logger.authenticate("hello")
let developmentLogger: AnalyticsLogger;
let clickEventProperties: ClickEventProperties = {
objectId: "object 1",
userId: "user 1"
}

let visitEventProperties: VisitEventProperties = {
pageUrl: "page 1",
userId: "user 1"
}

let inputEventProperties: InputEventProperties = {
objectId: "input 1",
userId: "user 1",
textValue: "text 1"
}

beforeAll(async () => {
developmentLogger = new AnalyticsLogger()
const project = await externalRequest<Project>({
url: urls.apiBaseUrl + urls.project,
method: HttpMethod.POST,
body: {
projectName: randomUUID()
}
})
developmentLogger.authenticate(project.clientApiKey, EventEnvironment.DEVELOPMENT);
})
test('Click Event Log', async () => {


test('Basic Click Event Test', async () => {
const clickEvent = await developmentLogger.logClickEvent(clickEventProperties)
expect(clickEvent?.eventProperties.objectId).toEqual(clickEventProperties.objectId)
expect(clickEvent?.eventProperties.userId).toEqual(clickEventProperties.userId)
})

test('Basic Visit Event Test', async () => {
const visitEvent = await developmentLogger.logVisitEvent(visitEventProperties)
expect(visitEvent?.eventProperties.pageUrl).toEqual(visitEventProperties.pageUrl)
expect(visitEvent?.eventProperties.userId).toEqual(visitEventProperties.userId)
})

test('Basic Input Event Test', async () => {
const inputEvent = await developmentLogger.logInputEvent(inputEventProperties)
expect(inputEvent?.eventProperties.objectId).toEqual(inputEventProperties.objectId)
expect(inputEvent?.eventProperties.userId).toEqual(inputEventProperties.userId)
expect(inputEvent?.eventProperties.textValue).toEqual(inputEventProperties.textValue)
})


})
3 changes: 2 additions & 1 deletion package/__tests__/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ describe('Slackbot Logger Module', () => {
await logMessage(formatErrorMessage("This is a jest test message for what an average error log might look like", {
"Project API Key": "Sample Project API Key",
"Object Id": "Sample Object Id",
"User Id": "Sample User Id"
"User Id": "Sample User Id",
"Environment": process.env.NODE_ENV as string
}))
})
})
7 changes: 4 additions & 3 deletions package/src/actions/click-event.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { externalRequest } from "@/utils/requests"
import { ClickEvent, HttpMethod } from "@/utils/types";
import { ClickEvent, EventEnvironment, HttpMethod } from "@/utils/types";
import { urls } from "@/utils/urls"

const clickEventUrl = urls.apiBaseUrl + urls.events.clickEvent;
export const createClickEvent = async (apiKey: string, objectId: string, userId: string): Promise<ClickEvent> => {
export const createClickEvent = async (apiKey: string, objectId: string, userId: string, environment: EventEnvironment): Promise<ClickEvent> => {
return externalRequest<ClickEvent>({
url: clickEventUrl,
method: HttpMethod.POST,
clientApiKey: apiKey,
body: {
objectId,
userId
userId,
environment
}
})
}
27 changes: 27 additions & 0 deletions package/src/actions/custom-event-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { externalRequest } from "@/utils/requests"
import { CustomEventType, HttpMethod } from "@/utils/types";
import { urls } from "@/utils/urls"

const customEventTypeUrl = urls.apiBaseUrl + urls.events.customEventType;
export const createCustomEventType = async (apiKey: string, category: string, subcategory: string, properties: string[]): Promise<CustomEventType> => {
return externalRequest<CustomEventType>({
url: customEventTypeUrl,
method: HttpMethod.POST,
serverApiKey: apiKey,
body: {
category,
subcategory,
properties
}
})
}

export const getCustomEventTypes = async (projectName: string): Promise<CustomEventType[]> => {
return externalRequest<CustomEventType[]>({
url: customEventTypeUrl,
method: HttpMethod.GET,
queryParams: {
projectName
}
})
}
17 changes: 17 additions & 0 deletions package/src/actions/custom-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { externalRequest } from "@/utils/requests"
import { CustomEvent, EventEnvironment, HttpMethod } from "@/utils/types";
import { urls } from "@/utils/urls"

const customEventUrl = urls.apiBaseUrl + urls.events.customEvent;
export const createCustomEvent = async (apiKey: string, eventTypeId: string, properties: object, environment: EventEnvironment): Promise<CustomEvent> => {
return externalRequest<CustomEvent>({
url: customEventUrl,
method: HttpMethod.POST,
clientApiKey: apiKey,
body: {
eventTypeId,
properties,
environment
}
})
}
29 changes: 29 additions & 0 deletions package/src/actions/custom-graph-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { externalRequest } from "@/utils/requests"
import { CustomGraphType, HttpMethod } from "@/utils/types";
import { urls } from "@/utils/urls"

export const customGraphTypeUrl = urls.apiBaseUrl + urls.events.customGraphType;
export const createCustomGraphType = async (apiKey: string, eventTypeId: string, xProperty: string, yProperty: string, graphType: string): Promise<CustomGraphType> => {
return externalRequest<CustomGraphType>({
url: customGraphTypeUrl,
method: HttpMethod.POST,
serverApiKey: apiKey,
body: {
eventTypeId,
xProperty,
yProperty,
graphType
}
})
}

export const getCustomGraphTypes = async (projectName: string, eventTypeId: string): Promise<CustomGraphType[]> => {
return externalRequest<CustomGraphType[]>({
url: customGraphTypeUrl,
method: HttpMethod.GET,
queryParams: {
projectName,
eventTypeId
}
})
}
18 changes: 18 additions & 0 deletions package/src/actions/input-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { externalRequest } from "@/utils/requests"
import { InputEvent, HttpMethod, EventEnvironment } from "@/utils/types";
import { urls } from "@/utils/urls"

const inputEventUrl = urls.apiBaseUrl + urls.events.inputEvent;
export const createInputEvent = async (apiKey: string, objectId: string, userId: string, textValue: string, environment: EventEnvironment): Promise<InputEvent> => {
return externalRequest<InputEvent>({
url: inputEventUrl,
method: HttpMethod.POST,
clientApiKey: apiKey,
body: {
objectId,
userId,
textValue,
environment
}
})
}
5 changes: 3 additions & 2 deletions package/src/actions/visit-event.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { externalRequest } from "@/utils/requests"
import { VisitEvent, HttpMethod } from "@/utils/types";
import { VisitEvent, HttpMethod, EventEnvironment } from "@/utils/types";
import { urls } from "@/utils/urls"

const visitEventUrl = urls.apiBaseUrl + urls.events.visitEvent;
export const createVisitEvent = async (apiKey: string, pageUrl: string, userId: string): Promise<VisitEvent> => {
export const createVisitEvent = async (apiKey: string, pageUrl: string, userId: string, environment: EventEnvironment): Promise<VisitEvent> => {
return externalRequest<VisitEvent>({
url: visitEventUrl,
method: HttpMethod.POST,
clientApiKey: apiKey,
body: {
pageUrl,
userId,
environment
}
})
}
Loading
Loading