Skip to content

Commit f2a5220

Browse files
Merge main (v2.1.0) into develop (#634)
* feat: allow users to turn analytics on and off (v2.1.0) (#632) * feat: allow users to turn analytics on and off (v2.1.0) This PR adds a new property to the SearchAnalyticsConfig, which can be used to start with analytics enabled or disabled by default. Three new methods are added to the analytics object- two to turn analytics on/off, and one to get the current enabled status. J=WAT-5404 TEST=auto, manual Ran test site locally with debugging and buttons to turn analytics on and off, saw expected behavior. * Update various names to be in-line with pages code * Automated update to repo's documentation from github action * Add enableYextAnalytics function to window * drop unneeded parens --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> * release: v2.1.0 --------- Co-authored-by: Fondryext <160865254+Fondryext@users.noreply.github.com> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Jacob Fondriest <jfondriest@yext.com>
1 parent 3d403a3 commit f2a5220

15 files changed

Lines changed: 178 additions & 17 deletions

docs/search-ui-react.searchanalyticsconfig.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,25 @@ Environment
9191
_(Optional)_ The Yext environment to send requests to. Defaults to 'PRODUCTION'.
9292

9393

94+
</td></tr>
95+
<tr><td>
96+
97+
[requireOptIn?](./search-ui-react.searchanalyticsconfig.requireoptin.md)
98+
99+
100+
</td><td>
101+
102+
103+
</td><td>
104+
105+
boolean
106+
107+
108+
</td><td>
109+
110+
_(Optional)_ Whether a user must consent to analytics tracking before events are fired. Defaults to false.
111+
112+
94113
</td></tr>
95114
<tr><td>
96115

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@yext/search-ui-react](./search-ui-react.md) &gt; [SearchAnalyticsConfig](./search-ui-react.searchanalyticsconfig.md) &gt; [requireOptIn](./search-ui-react.searchanalyticsconfig.requireoptin.md)
4+
5+
## SearchAnalyticsConfig.requireOptIn property
6+
7+
Whether a user must consent to analytics tracking before events are fired. Defaults to false.
8+
9+
**Signature:**
10+
11+
```typescript
12+
requireOptIn?: boolean;
13+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@yext/search-ui-react](./search-ui-react.md) &gt; [SearchAnalyticsEventService](./search-ui-react.searchanalyticseventservice.md) &gt; [isYextAnalyticsEnabled](./search-ui-react.searchanalyticseventservice.isyextanalyticsenabled.md)
4+
5+
## SearchAnalyticsEventService.isYextAnalyticsEnabled() method
6+
7+
Returns whether analytics reporting is currently enabled
8+
9+
**Signature:**
10+
11+
```typescript
12+
isYextAnalyticsEnabled(): boolean;
13+
```
14+
**Returns:**
15+
16+
boolean
17+

docs/search-ui-react.searchanalyticseventservice.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,39 @@ Description
2727
</th></tr></thead>
2828
<tbody><tr><td>
2929

30+
[isYextAnalyticsEnabled()](./search-ui-react.searchanalyticseventservice.isyextanalyticsenabled.md)
31+
32+
33+
</td><td>
34+
35+
Returns whether analytics reporting is currently enabled
36+
37+
38+
</td></tr>
39+
<tr><td>
40+
41+
[optIn()](./search-ui-react.searchanalyticseventservice.optin.md)
42+
43+
44+
</td><td>
45+
46+
Enables analytics reporting
47+
48+
49+
</td></tr>
50+
<tr><td>
51+
52+
[optOut()](./search-ui-react.searchanalyticseventservice.optout.md)
53+
54+
55+
</td><td>
56+
57+
Disables analytics reporting
58+
59+
60+
</td></tr>
61+
<tr><td>
62+
3063
[report(payload)](./search-ui-react.searchanalyticseventservice.report.md)
3164

3265

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@yext/search-ui-react](./search-ui-react.md) &gt; [SearchAnalyticsEventService](./search-ui-react.searchanalyticseventservice.md) &gt; [optIn](./search-ui-react.searchanalyticseventservice.optin.md)
4+
5+
## SearchAnalyticsEventService.optIn() method
6+
7+
Enables analytics reporting
8+
9+
**Signature:**
10+
11+
```typescript
12+
optIn(): void;
13+
```
14+
**Returns:**
15+
16+
void
17+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@yext/search-ui-react](./search-ui-react.md) &gt; [SearchAnalyticsEventService](./search-ui-react.searchanalyticseventservice.md) &gt; [optOut](./search-ui-react.searchanalyticseventservice.optout.md)
4+
5+
## SearchAnalyticsEventService.optOut() method
6+
7+
Disables analytics reporting
8+
9+
**Signature:**
10+
11+
```typescript
12+
optOut(): void;
13+
```
14+
**Returns:**
15+
16+
void
17+

etc/search-ui-react.api.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,11 +689,15 @@ export interface SearchAnalyticsConfig {
689689
apiKey: string;
690690
cloudRegion?: CloudRegion;
691691
environment?: Environment;
692+
requireOptIn?: boolean;
692693
sessionTrackingEnabled?: boolean;
693694
}
694695

695696
// @public
696697
export interface SearchAnalyticsEventService {
698+
isYextAnalyticsEnabled(): boolean;
699+
optIn(): void;
700+
optOut(): void;
697701
// Warning: (ae-forgotten-export) The symbol "SearchEventPayload" needs to be exported by the entry point index.d.ts
698702
report(payload: SearchEventPayload): Promise<string>;
699703
}
@@ -1039,7 +1043,7 @@ export interface VisualAutocompleteConfig {
10391043

10401044
// Warnings were encountered during analysis:
10411045
//
1042-
// dist/index.d.ts:1741:5 - (ae-forgotten-export) The symbol "translations" needs to be exported by the entry point index.d.ts
1046+
// dist/index.d.ts:1749:5 - (ae-forgotten-export) The symbol "translations" needs to be exported by the entry point index.d.ts
10431047

10441048
// (No @packageDocumentation comment for this package)
10451049

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@yext/search-ui-react",
3-
"version": "2.0.5",
3+
"version": "2.1.0",
44
"description": "A library of React Components for powering Yext Search integrations",
55
"author": "watson@yext.com",
66
"license": "BSD-3-Clause",

src/components/AnalyticsProvider.tsx

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import React, { PropsWithChildren } from 'react';
1+
import React, { PropsWithChildren, useEffect, useRef } from 'react';
22
import { AnalyticsContext } from '../hooks/useAnalytics';
33
import { CloudRegion, Environment } from '@yext/search-core';
44
import { SearchAnalyticsEventServiceImpl } from '../models/SearchAnalyticsEventServiceImpl';
5+
import { SearchAnalyticsEventService } from '../models';
56

67
/**
78
* A higher-order component which provides analytics for its children.
@@ -13,11 +14,28 @@ import { SearchAnalyticsEventServiceImpl } from '../models/SearchAnalyticsEventS
1314
*/
1415
export function AnalyticsProvider(props: PropsWithChildren<SearchAnalyticsConfig>): React.JSX.Element {
1516
const { children, ...searchAnalyticsConfig } = props;
16-
const analyticsReporter =
17-
new SearchAnalyticsEventServiceImpl(searchAnalyticsConfig);
17+
18+
const analyticsRef = useRef<SearchAnalyticsEventService | null>(null);
19+
20+
if (analyticsRef.current === null) {
21+
analyticsRef.current = new SearchAnalyticsEventServiceImpl(searchAnalyticsConfig);
22+
}
23+
24+
const analytics = analyticsRef.current;
25+
26+
// Adds enableYextAnalytics to the window. Typically used during consent banner implementation.
27+
useEffect(() => {
28+
(window as any).enableYextAnalytics = () => {
29+
analytics.optIn();
30+
};
31+
32+
return () => {
33+
delete (window as any).enableYextAnalytics;
34+
};
35+
}, [analytics]);
1836

1937
return (
20-
<AnalyticsContext.Provider value={ analyticsReporter }>
38+
<AnalyticsContext.Provider value={ analytics }>
2139
{children}
2240
</AnalyticsContext.Provider>
2341
);
@@ -36,5 +54,7 @@ export interface SearchAnalyticsConfig {
3654
/** The region to send requests to. Defaults to 'US'. */
3755
cloudRegion?: CloudRegion,
3856
/** Whether to enable session tracking for analytics events. */
39-
sessionTrackingEnabled?: boolean
57+
sessionTrackingEnabled?: boolean,
58+
/** Whether a user must consent to analytics tracking before events are fired. Defaults to false. */
59+
requireOptIn?: boolean
4060
}

0 commit comments

Comments
 (0)