Skip to content

Commit

Permalink
Merge pull request #2 from HAKASHUN/master
Browse files Browse the repository at this point in the history
add config for amplitude initialization

Thanks for the PR
  • Loading branch information
koblas authored Apr 15, 2020
2 parents 9422f99 + 8b6408a commit 200222c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions src/components/AmplitudeProvider.tsx
Original file line number Diff line number Diff line change
@@ -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;
};

Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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
};

0 comments on commit 200222c

Please sign in to comment.