I'm trying to integrate Heap's snippet to my VueJS app. But I have a staging and a production environment so the key must be dynamic using .env
which is impossible when loading it in index.html
. So, I tried to make it a Vue plugin:
//main.js
import heap from "./plugins/heap";
Vue.use(heap, {key: process.env.VUE_APP_HEAP_ID});
// heap.js
export const heapinit = (e, t) => { [...] } // heap's init script
export default {
install(Vue, options) {
heapinit(options.key);
}
}
This seemed to work as I saw 200s responses from Heap in the network tab showing successful calls. But in the Live data screen on Heap the event's were not captured. I also tried this NPM package called vue-heap that attempts to solve it, but it also has the same issue.
So more generally, what are the best practices for a VueJS app for integrating JS snippets which should not live in index.html
but in main.js
and that don't have an NPM package? Another example: HubSpot tracking code
I'm learning Vue so feel free to correct any of the above logic if it's wrong! Thank you :)