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

chore: add internalUsageAttributionIds in the library #682

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ const App = () => {
);
};
```
### Internal usage attribution ID

This library uses [`internalUsageAttributionIds`](https://developers.google.com/maps/documentation/javascript/reference/map#MapOptions.internalUsageAttributionIds), which helps Google understand which libraries and
samples are helpful to developers and is optional.

To opt-out, use
```
<APIProvider useInternalUsageAttributionIds=false>
<MyComponent />
</APIProvider>
```

## Examples

Expand Down
31 changes: 26 additions & 5 deletions src/components/api-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export interface APIProviderContextValue {
}

const DEFAULT_SOLUTION_CHANNEL = 'GMP_visgl_rgmlibrary_v1_default';
const DEFAULT_INTERNAL_USAGE_ATTRIBUTION_IDS = [
'GMP_LIB_VISGL_REACT_GOOGLE_MAPS',
];

export const APIProviderContext =
React.createContext<APIProviderContextValue | null>(null);
Expand Down Expand Up @@ -81,6 +84,12 @@ export type APIProviderProps = PropsWithChildren<{
* [documentation](https://developers.google.com/maps/reporting-and-monitoring/reporting#usage-tracking-per-channel)
*/
solutionChannel?: string;
/**
* To help Google understand which libraries and samples are helpful to developers, such as usage of this library. To opt out of sending the usage attribution ID, it is safe to set this to `false`.
* Read more in the
* [documentation](https://developers.google.com/maps/documentation/javascript/reference/map#MapOptions.internalUsageAttributionIds)
*/
useInternalUsageAttributionIds?: boolean;
/**
* A function that can be used to execute code after the Google Maps JavaScript API has been loaded.
*/
Expand Down Expand Up @@ -126,6 +135,7 @@ function useGoogleMapsApiLoader(props: APIProviderProps) {
apiKey,
version,
libraries = [],
useInternalUsageAttributionIds = true,
...otherApiParams
} = props;

Expand Down Expand Up @@ -171,6 +181,14 @@ function useGoogleMapsApiLoader(props: APIProviderProps) {
[loadedLibraries]
);

const internalUsageAttributionIds = useMemo(
() =>
useInternalUsageAttributionIds
? DEFAULT_INTERNAL_USAGE_ATTRIBUTION_IDS
: null,
[useInternalUsageAttributionIds],
);

useEffect(
() => {
(async () => {
Expand Down Expand Up @@ -218,7 +236,8 @@ function useGoogleMapsApiLoader(props: APIProviderProps) {
return {
status,
loadedLibraries,
importLibrary
importLibrary,
internalUsageAttributionIds
};
}

Expand All @@ -230,7 +249,7 @@ export const APIProvider: FunctionComponent<APIProviderProps> = props => {
const {mapInstances, addMapInstance, removeMapInstance, clearMapInstances} =
useMapInstances();

const {status, loadedLibraries, importLibrary} =
const {status, loadedLibraries, importLibrary, internalUsageAttributionIds} =
useGoogleMapsApiLoader(loaderProps);

const contextValue: APIProviderContextValue = useMemo(
Expand All @@ -241,7 +260,8 @@ export const APIProvider: FunctionComponent<APIProviderProps> = props => {
clearMapInstances,
status,
loadedLibraries,
importLibrary
importLibrary,
internalUsageAttributionIds
}),
[
mapInstances,
Expand All @@ -250,8 +270,9 @@ export const APIProvider: FunctionComponent<APIProviderProps> = props => {
clearMapInstances,
status,
loadedLibraries,
importLibrary
]
importLibrary,
internalUsageAttributionIds
],
);

return (
Expand Down
2 changes: 2 additions & 0 deletions src/components/map/use-map-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
if (!mapOptions.tilt && Number.isFinite(defaultTilt))
mapOptions.tilt = defaultTilt;

mapOptions.internalUsageAttributionIds = context.internalUsageAttributionIds;

Check failure on line 104 in src/components/map/use-map-instance.ts

View workflow job for this annotation

GitHub Actions / test

Property 'internalUsageAttributionIds' does not exist on type '{ center?: LatLng | LatLngLiteral | null | undefined; backgroundColor?: string | null | undefined; cameraControl?: boolean | null | undefined; cameraControlOptions?: CameraControlOptions | ... 1 more ... | undefined; ... 66 more ...; children?: ReactNode; }'.

Check failure on line 104 in src/components/map/use-map-instance.ts

View workflow job for this annotation

GitHub Actions / test

Property 'internalUsageAttributionIds' does not exist on type 'APIProviderContextValue'.

for (const key of Object.keys(mapOptions) as (keyof typeof mapOptions)[])
if (mapOptions[key] === undefined) delete mapOptions[key];

Expand Down
Loading