From 8b6408a1e6495292052068621d272a8efad91206 Mon Sep 17 00:00:00 2001 From: HAKASHUN Date: Wed, 15 Apr 2020 17:30:06 +0900 Subject: [PATCH] add config for amplitude initialization --- package-lock.json | 2 +- src/components/AmplitudeProvider.tsx | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index c229fca..8ebb26d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "react-amplitude-hooks", - "version": "0.0.1", + "version": "0.9.11", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/components/AmplitudeProvider.tsx b/src/components/AmplitudeProvider.tsx index 08426fb..0fafb50 100644 --- a/src/components/AmplitudeProvider.tsx +++ b/src/components/AmplitudeProvider.tsx @@ -1,12 +1,13 @@ import * as PropTypes from "prop-types"; import * as React from "react"; import { isValidAmplitudeInstance } from "../lib/validation"; -import { AmplitudeClient } from "amplitude-js"; +import {AmplitudeClient, Config} from "amplitude-js"; declare type Props = { amplitudeInstance: AmplitudeClient; apiKey: string; userId?: string; + config?: Config; children: React.ReactNode; }; @@ -24,11 +25,11 @@ export function useAmplitudeContext() { return React.useContext(AmplitudeContext); } -function initAmplitude(apiKey: string, userId: any, amplitudeInstance: AmplitudeClient) { +function initAmplitude(amplitudeInstance: AmplitudeClient, apiKey: string, userId?: string, config?: Config) { return () => { if (isValidAmplitudeInstance(amplitudeInstance)) { if (apiKey) { - amplitudeInstance.init(apiKey); + amplitudeInstance.init(apiKey, undefined, config); } if (userId) { amplitudeInstance.setUserId(userId); @@ -38,13 +39,14 @@ function initAmplitude(apiKey: string, userId: any, amplitudeInstance: Amplitude } export function AmplitudeProvider(props: Props) { - const { apiKey, userId, amplitudeInstance } = props; + const { amplitudeInstance, apiKey, userId, config } = props; // Memoize so it's only really called if the params change - const init = React.useMemo(() => initAmplitude(apiKey, userId, amplitudeInstance), [ + const init = React.useMemo(() => initAmplitude(amplitudeInstance, apiKey, userId, config), [ + amplitudeInstance, apiKey, userId, - amplitudeInstance + config, ]); // We need to init such that LogOnMount is happy @@ -65,5 +67,6 @@ export function AmplitudeProvider(props: Props) { AmplitudeProvider.propTypes = { amplitudeInstance: PropTypes.object.isRequired, apiKey: PropTypes.string, - userId: PropTypes.string + userId: PropTypes.string, + config: PropTypes.object };