Skip to content

Commit

Permalink
fix: some refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
JackHamer09 committed Jan 18, 2024
1 parent 3a33d94 commit 8d99b0b
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 65 deletions.
4 changes: 1 addition & 3 deletions .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
WALLET_CONNECT_PROJECT_ID=373a8744ceaac00934aec708a3fceea6
ANKR_TOKEN=
DATAPLANE_URL=
RUDDER_KEY=
ANKR_TOKEN=
2 changes: 2 additions & 0 deletions .github/workflows/feature.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ jobs:
echo "WALLET_CONNECT_PROJECT_ID=${{ secrets.WALLET_CONNECT_PROJECT_ID }}" > .env
echo "ANKR_TOKEN=${{ secrets.ANKR_TOKEN }}" >> .env
echo "SCREENING_API_URL=${{ secrets.SCREENING_API_URL }}" >> .env
echo "DATAPLANE_URL=${{ secrets.DATAPLANE_URL }}" >> .env
echo "RUDDER_KEY=${{ secrets.RUDDER_KEY }}" >> .env
- name: Build
run: |
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ jobs:
echo "WALLET_CONNECT_PROJECT_ID=${{ secrets.WALLET_CONNECT_PROJECT_ID }}" > .env
echo "ANKR_TOKEN=${{ secrets.ANKR_TOKEN }}" >> .env
echo "SCREENING_API_URL=${{ secrets.SCREENING_API_URL }}" >> .env
echo "DATAPLANE_URL=${{ secrets.DATAPLANE_URL }}" >> .env
echo "RUDDER_KEY=${{ secrets.RUDDER_KEY }}" >> .env
- name: Build
run: |
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ jobs:
echo "WALLET_CONNECT_PROJECT_ID=${{ secrets.WALLET_CONNECT_PROJECT_ID }}" > .env
echo "ANKR_TOKEN=${{ secrets.ANKR_TOKEN }}" >> .env
echo "SCREENING_API_URL=${{ secrets.SCREENING_API_URL }}" >> .env
echo "DATAPLANE_URL=${{ secrets.DATAPLANE_URL }}" >> .env
echo "RUDDER_KEY=${{ secrets.RUDDER_KEY }}" >> .env
- name: Build
run: |
Expand Down
21 changes: 10 additions & 11 deletions app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
</template>

<script lang="ts" setup>
import { trackPage } from "~/utils/analytics";
trackPage();
</script>
import { watch } from "vue";
import { useRoute } from "vue-router";
<style lang="scss"></style>
import { trackPage } from "@/utils/analytics";
<script lang="ts">
export default {
watch: {
$route() {
trackPage();
},
const route = useRoute();
watch(
() => route.path,
() => {
trackPage();
},
};
{ immediate: true }
);
</script>
27 changes: 0 additions & 27 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,6 @@ export default defineNuxtConfig({
src: "http://cdn.rudderlabs.com/v1.1/rudder-analytics.min.js",
defer: true,
},
{
hid: "rudder-js",
innerHTML: `
rudderanalytics = window.rudderanalytics = [];
var methods = [
'load',
'page',
'track',
'identify',
'alias',
'group',
'ready',
'reset',
'getAnonymousId',
'setAnonymousId'
];
for (var i = 0; i < methods.length; i++) {
var method = methods[i];
rudderanalytics[method] = function (methodName) {
return function () {
rudderanalytics.push([methodName].concat(Array.prototype.slice.call(arguments)));
};
}(method);
}
`,
type: "text/javascript",
},
],
},
},
Expand Down
7 changes: 7 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,12 @@ declare global {
) => string | undefined;
reset: (widgetId: string) => void;
};
rudderanalytics?: {
load: (key: string, url: string) => void;
ready: (callback: () => void) => void;
page: () => void;
track: (eventName: string, params?: unknown) => void;
initialized: boolean;
};
}
}
54 changes: 30 additions & 24 deletions utils/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let rudderanalytics: any = null;
let analyticsLoaded = false;

export async function initAnalytics(): Promise<boolean> {
const runtimeConfig = useRuntimeConfig();

if (runtimeConfig.public.rudderKey && runtimeConfig.public.dataplaneUrl) {
if (rudderanalytics) {
return true;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
while (!(window as any).rudderanalytics) {
await setTimeout(() => {
return;
}, 500);
const isReady = (): Promise<void> => {
return new Promise((resolve, reject) => {
if (!window.rudderanalytics) {
reject(new Error("Rudder not loaded"));
}
window.rudderanalytics?.ready(() => {
resolve();
});
});
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
rudderanalytics = (window as any).rudderanalytics;
export async function initAnalytics(): Promise<boolean> {
const runtimeConfig = useRuntimeConfig();
if (!runtimeConfig.public.rudderKey || !runtimeConfig.public.dataplaneUrl) {
return false;
}

await rudderanalytics.load(runtimeConfig.public.rudderKey, runtimeConfig.public.dataplaneUrl);
await rudderanalytics.ready();
if (analyticsLoaded) {
await isReady();
return true;
}
return false;
await retry(async () => {
if (!window.rudderanalytics) {
await new Promise((resolve) => setTimeout(resolve, 250));
throw new Error("Rudder not loaded");
}
});
window.rudderanalytics?.load(runtimeConfig.public.rudderKey, runtimeConfig.public.dataplaneUrl);
analyticsLoaded = true;
await isReady();
return true;
}

export async function trackPage(): Promise<void> {
if (await initAnalytics()) {
await rudderanalytics.page();
window.rudderanalytics?.page();
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function trackEvent(eventName: string, params?: any): Promise<void> {
export async function trackEvent(eventName: string, params?: unknown): Promise<void> {
if (await initAnalytics()) {
await rudderanalytics.track(eventName, params);
window.rudderanalytics?.track(eventName, params);
}
}

0 comments on commit 8d99b0b

Please sign in to comment.