Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia
TypeScript apiapi-clientapi-restapi-testingdeveloper-toolsgraphqlhttphttp-clientpwarestrest-apispatestingtesting-toolstoolsvuevuejswebsocket
feat: add analytics logging while hoppscotch requests are fired
Andrew Bastin86bd4aa568c3
diff --git a/components/realtime/Mqtt.vue b/components/realtime/Mqtt.vueindex 189cd0c98..0ff047bf1 100644--- a/components/realtime/Mqtt.vue+++ b/components/realtime/Mqtt.vue@@ -121,6 +121,7 @@<script>import Paho from "paho-mqtt"import debounce from "~/helpers/utils/debounce"+import { logHoppRequestRunToAnalytics } from "~/helpers/fb/analytics"export default {data() {@@ -191,6 +192,10 @@ export default {})this.client.onConnectionLost = this.onConnectionLostthis.client.onMessageArrived = this.onMessageArrived++ logHoppRequestRunToAnalytics({+ platform: "mqtt",+ })},onConnectionFailure() {this.connectionState = falsediff --git a/components/realtime/Socketio.vue b/components/realtime/Socketio.vueindex 40e72eadd..0ee080f0c 100644--- a/components/realtime/Socketio.vue+++ b/components/realtime/Socketio.vue@@ -141,6 +141,7 @@import { io as Client } from "socket.io-client"import wildcard from "socketio-wildcard"import debounce from "~/helpers/utils/debounce"+import { logHoppRequestRunToAnalytics } from "~/helpers/fb/analytics"export default {data() {@@ -262,6 +263,10 @@ export default {icon: "error",})}++ logHoppRequestRunToAnalytics({+ platform: "socketio",+ })},disconnect() {this.io.close()diff --git a/components/realtime/Sse.vue b/components/realtime/Sse.vueindex 5bc903165..eebea9c36 100644--- a/components/realtime/Sse.vue+++ b/components/realtime/Sse.vue@@ -48,6 +48,7 @@</template><script>+import { logHoppRequestRunToAnalytics } from "~/helpers/fb/analytics"import debounce from "~/helpers/utils/debounce"export default {@@ -158,6 +159,10 @@ export default {},]}++ logHoppRequestRunToAnalytics({+ platform: "mqtt",+ })},handleSSEError(error) {this.stop()diff --git a/components/realtime/Websocket.vue b/components/realtime/Websocket.vueindex c4e7cf12b..f089e7c87 100644--- a/components/realtime/Websocket.vue+++ b/components/realtime/Websocket.vue@@ -159,6 +159,7 @@</template><script>+import { logHoppRequestRunToAnalytics } from "~/helpers/fb/analytics"import debounce from "~/helpers/utils/debounce"export default {@@ -273,6 +274,10 @@ export default {icon: "error",})}++ logHoppRequestRunToAnalytics({+ platform: "wss",+ })},disconnect() {if (this.socket) {diff --git a/helpers/fb/analytics.ts b/helpers/fb/analytics.tsindex 839b1448d..f5eb8ad51 100644--- a/helpers/fb/analytics.ts+++ b/helpers/fb/analytics.ts@@ -13,6 +13,13 @@ type SettingsCustomDimensions = {syncHistory: boolean}+type HoppRequestEvent =+ | {+ platform: "rest" | "graphql-query" | "graphql-schema"+ strategy: "normal" | "proxy" | "extension"+ }+ | { platform: "wss" | "sse" | "socketio" | "mqtt" }+export function initAnalytics() {analytics = firebase.app().analytics()@@ -46,3 +53,7 @@ function initSettingsListeners() {})})}++export function logHoppRequestRunToAnalytics(ev: HoppRequestEvent) {+ analytics.logEvent("hopp-request", ev)+}diff --git a/helpers/network.js b/helpers/network.jsindex 4ba209b03..ca291440e 100644--- a/helpers/network.js+++ b/helpers/network.js@@ -25,5 +25,21 @@ const runAppropriateStrategy = (req) => {return AxiosStrategy(req)}+/**+ * Returns an identifier for how a request will be ran+ * if the system is asked to fire a request+ *+ * @returns {"normal" | "extension" | "proxy"}+ */+export function getCurrentStrategyID() {+ if (isExtensionsAllowed() && hasExtensionInstalled()) {+ return "extension"+ } else if (settingsStore.value.PROXY_ENABLED) {+ return "proxy"+ } else {+ return "normal"+ }+}+export const sendNetworkRequest = (req) =>runAppropriateStrategy(req).finally(() => window.$nuxt.$loading.finish())diff --git a/pages/graphql.vue b/pages/graphql.vueindex 6ec239bff..9dc899aa2 100644--- a/pages/graphql.vue+++ b/pages/graphql.vue@@ -485,9 +485,10 @@import * as gql from "graphql"import { commonHeaders } from "~/helpers/headers"import { getPlatformSpecialKey } from "~/helpers/platformutils"-import { sendNetworkRequest } from "~/helpers/network"+import { getCurrentStrategyID, sendNetworkRequest } from "~/helpers/network"import { getSettingSubject } from "~/newstore/settings"import { addGraphqlHistoryEntry } from "~/newstore/history"+import { logHoppRequestRunToAnalytics } from "~/helpers/fb/analytics"export default {beforeRouteLeave(_to, _from, next) {@@ -849,6 +850,11 @@ export default {})console.log("Error", error)}++ logHoppRequestRunToAnalytics({+ platform: "graphql-query",+ strategy: getCurrentStrategyID(),+ })},// NOTE : schema required here is the GQL Schema document object, not the schema string@@ -918,6 +924,11 @@ export default {await this.getSchema()this.pollSchema()++ logHoppRequestRunToAnalytics({+ platform: "graphql-schema",+ strategy: getCurrentStrategyID(),+ })}},async pollSchema() {diff --git a/pages/index.vue b/pages/index.vueindex f69c8a7b7..d1a593acd 100644--- a/pages/index.vue+++ b/pages/index.vue@@ -766,7 +766,11 @@ import getEnvironmentVariablesFromScript from "~/helpers/preRequest"import runTestScriptWithVariables from "~/helpers/postwomanTesting"import parseTemplateString from "~/helpers/templating"import { tokenRequest, oauthRedirect } from "~/helpers/oauth"-import { cancelRunningRequest, sendNetworkRequest } from "~/helpers/network"+import {+ cancelRunningRequest,+ getCurrentStrategyID,+ sendNetworkRequest,+} from "~/helpers/network"import {hasPathParams,addPathParamsToVariables,@@ -782,6 +786,7 @@ import { generateCodeWithGenerator } from "~/helpers/codegen/codegen"import { getSettingSubject, applySetting } from "~/newstore/settings"import { addRESTHistoryEntry } from "~/newstore/history"import clone from "lodash/clone"+import { logHoppRequestRunToAnalytics } from "~/helpers/fb/analytics"export default {data() {@@ -1642,6 +1647,11 @@ export default {response: syntheticResponse,})this.testReports = testResults++ logHoppRequestRunToAnalytics({+ platform: "rest",+ strategy: getCurrentStrategyID(),+ })},getQueryStringFromPath() {const pathParsed = url.parse(this.uri)