Skip to content

Commit 2eabc05

Browse files
author
Purnima
authored
Merge pull request #2 from booleanhunter/fix/hoc-context
refactor(UserInteractionResource): Change context prop type
2 parents c2497c5 + 6302185 commit 2eabc05

File tree

5 files changed

+53
-18
lines changed

5 files changed

+53
-18
lines changed

src/components/templates/LandingPageOne/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,12 @@ function LandingPage(props: LandingPageProps) {
118118

119119
// optional props
120120
origin="Landing Page"
121-
// context="AntD Card"
121+
dataContext={{
122+
app: {
123+
version: "0",
124+
},
125+
context: "Custom Landing page"
126+
}}
122127
/>,
123128
]}>
124129

src/components/templates/LoginForm/index.tsx

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ function LoginForm(props: LoginFormProps) {
6363

6464
// optional props
6565
origin="Login Button"
66-
// context="AntD Card"
66+
dataContext={{
67+
app: {
68+
version: "0",
69+
},
70+
context: "Custom login page"
71+
}}
6772
/>,
6873
<Button
6974
type="ghost"
@@ -75,16 +80,31 @@ function LoginForm(props: LoginFormProps) {
7580
type="text"
7681
placeholder="email"
7782

78-
trackers={[{
79-
action: "onClick",
80-
track: logEvent,
81-
data: {
82-
customKey1: "input",
83+
trackers={[
84+
{
85+
action: "onClick",
86+
track: logEvent,
87+
data: {
88+
customKey1: "input",
89+
}
90+
},
91+
{
92+
action: "onChange",
93+
track: logEvent,
94+
data: {
95+
customKey1: "input",
96+
}
8397
}
84-
}]}
98+
]}
8599

86100
// optional props
87-
origin="Email Input"
101+
origin="Login Page Email Input"
102+
dataContext={{
103+
app: {
104+
version: "0",
105+
},
106+
context: "Custom login page"
107+
}}
88108
/>
89109
<Input
90110
type="password"

src/library/user-analytics/lib/interaction-tracking/browser.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
export function getUserOS() {
1+
interface OSDetails{
2+
name: string;
3+
version: string;
4+
}
5+
6+
export function getUserOS() : OSDetails {
27
const userAgent = window.navigator.userAgent;
38
const platform = window.navigator.platform;
49
const macosPlatforms = ["Macintosh", "MacIntel", "MacPPC", "Mac68K"];

src/library/user-analytics/lib/resources/userInteractionResource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default interface UserInteractionResource extends BaseResource {
1111
action: UserInteraction.Action;
1212
source: {
1313
context: string;
14-
origin: string;
14+
origin?: string;
1515
component: string;
1616
element: {
1717
currentTarget: string;

src/library/user-analytics/react/components/withTracking.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { DataContext } from '../contexts/dataContext';
99

1010
type TrackerProps = {
11-
context?: string,
11+
dataContext?: UserInteraction.DataContext;
1212
origin?: string,
1313
trackers: UserInteraction.Tracker[]
1414
} & Object<any>;
@@ -18,10 +18,13 @@ export function withTracking (
1818
) {
1919
return function Fn(props: TrackerProps) {
2020
let eventHandlers: Object<any> = {};
21-
const { trackers, ...originalProps } = props;
21+
const { trackers, origin, dataContext: dataContextFromProps, ...originalProps } = props;
2222

23-
const dataContext = useContext(DataContext);
24-
const context = props.context ? props.context : dataContext.context;
23+
let dataContext = useContext(DataContext);
24+
25+
if (dataContextFromProps) {
26+
dataContext = dataContextFromProps
27+
}
2528

2629
function trackUserInteraction(
2730
e: React.SyntheticEvent,
@@ -33,8 +36,10 @@ export function withTracking (
3336
dataContext.app,
3437
tracker.action,
3538
{
36-
context,
37-
origin: originalProps.origin || "",
39+
context: dataContext.context,
40+
...(originalProps.origin && {
41+
origin: originalProps.origin
42+
}),
3843
component: Component.displayName || Component.name,
3944
element: {
4045
currentTarget: e.currentTarget.nodeName,
@@ -63,7 +68,7 @@ export function withTracking (
6368

6469
return (
6570
<Component
66-
{ ...props }
71+
{ ...originalProps }
6772
{ ...eventHandlers }
6873
/>
6974
);

0 commit comments

Comments
 (0)