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

save user._id to storage after track, and use in conversion event #190

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion src/api/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Http from '../http';
import Logger from '../logger';
import Session from '../session';
import Navigator from '../navigator';
import Storage from '../storage';

import type { RadarTrackParams } from '../types';

Expand All @@ -16,12 +17,14 @@ class ConfigAPI {
return;
}

const id = Storage.getItem(Storage.ID) || undefined;
const deviceId = params.deviceId || Device.getDeviceId();
const installId = params.installId || Device.getInstallId();
const sessionId = Session.getSessionId();
const locationAuthorization = await Navigator.getPermissionStatus();

const data = {
const data: any = {
id,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just to verify, id is the correct param (not _id)?

deviceId,
installId,
sessionId,
Expand Down
9 changes: 6 additions & 3 deletions src/api/conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ class ConversionsAPI {
const options = Config.get();

const name = params.name;
const userId = params.userId || Storage.getItem(Storage.USER_ID);
const deviceId = params.deviceId || Device.getDeviceId();
const installId = params.installId || Device.getInstallId();
const metadata = params.metadata || {};
const createdAt = params.createdAt;

const id = Storage.getItem(Storage.ID);
const userId = params.userId || Storage.getItem(Storage.USER_ID);
const deviceId = params.deviceId || Device.getDeviceId();
const installId = params.installId || Device.getInstallId();

if (params.revenue) {
metadata.revenue = params.revenue;
}

const data: any = {
id,
name,
userId,
deviceId,
Expand Down
10 changes: 10 additions & 0 deletions src/api/track.ts
Copy link

Choose a reason for hiding this comment

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

Should also send _id up in track if it exists (makes subsequent tracks more performant).

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class TrackAPI {
}

// user indentification fields
const id = Storage.getItem(Storage.ID) || undefined; // `undefined` so it's removed from any fields
const userId = params.userId || Storage.getItem(Storage.USER_ID);
const deviceId = params.deviceId || Device.getDeviceId();
const installId = params.installId || Device.getInstallId();
Expand Down Expand Up @@ -84,6 +85,7 @@ class TrackAPI {
userId,
tripOptions,
timeZone,
id,
};

let response: any;
Expand All @@ -99,6 +101,7 @@ class TrackAPI {
method: 'GET',
path: 'config',
data: {
id,
deviceId,
installId,
sessionId,
Expand Down Expand Up @@ -182,13 +185,18 @@ class TrackAPI {
_id,
} as RadarTrackVerifiedResponse;

if (user) {
Storage.setItem(Storage.ID, user._id);
}

if (options.debug) {
trackRes.response = response;
}

return trackRes;
}

// unverified track
response = await Http.request({
method: 'POST',
path: 'track',
Expand All @@ -204,6 +212,8 @@ class TrackAPI {
location,
} as RadarTrackResponse;

Storage.setItem(Storage.ID, user?._id);

if (options.debug) {
trackRes.response = response;
}
Expand Down
11 changes: 11 additions & 0 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import Logger from './logger';
class Storage {

// local storage key definitions for identifying track users
public static get ID() {
return 'radar-id';
}
public static get USER_ID() {
return 'radar-userId';
}
Expand Down Expand Up @@ -41,6 +44,14 @@ class Storage {
if (!storage) {
return;
}
// clear id if user_id doesn't match previous
if (key == this.USER_ID) {
const previousUserId = this.getItem(this.USER_ID);
if (previousUserId !== value) {
storage.removeItem(this.ID);
console.log("User id mismatch, removing ID");
}
}
if (value === undefined || value === null) {
return;
}
Expand Down
Loading