Skip to content

Commit

Permalink
sjekk på om amplitude er tilgjengelig
Browse files Browse the repository at this point in the history
  • Loading branch information
frodehansen2 committed Jan 28, 2025
1 parent dd633b6 commit f89f39a
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions packages/sif-common-amplitude/src/amplitude.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useEffect } from 'react';
import * as amplitude from '@amplitude/analytics-browser';
import constate from 'constate';

const MAX_AWAIT_TIME = 500;
Expand Down Expand Up @@ -60,7 +59,7 @@ type EventProperties = {
[key: string]: any;
};

export const initAmplitude = (apiKey = 'default') => {
export const initAmplitude = (amplitude, apiKey = 'default') => {
amplitude.init(apiKey, undefined, {
serverUrl: 'https://amplitude.nav.no/collect-auto',
defaultTracking: false,
Expand All @@ -74,14 +73,17 @@ export const initAmplitude = (apiKey = 'default') => {
export const [AmplitudeProvider, useAmplitudeInstance] = constate((props: Props) => {
const { applicationKey, isActive = true, maxAwaitTime = MAX_AWAIT_TIME, logToConsoleOnly, apiKey } = props;

const amplitude = (window as any)?.dekoratorenAmplitude;
const enabled = amplitude !== undefined;

useEffect(() => {
if (isActive) {
initAmplitude(apiKey);
if (isActive && enabled) {
initAmplitude(amplitude, apiKey);
}
}, [isActive]);
}, [isActive, enabled]);

async function logEvent(eventName: SIFCommonGeneralEvents | string, eventProperties?: EventProperties) {
if (isActive) {
if (isActive && enabled) {
const timeoutPromise = new Promise((resolve) => setTimeout(() => resolve(null), maxAwaitTime));
const logPromise = new Promise((resolve) => {
const eventProps = { ...eventProperties, app: applicationKey, applikasjon: applicationKey };
Expand Down

0 comments on commit f89f39a

Please sign in to comment.