Solved

heap.config.json with env vars

  • 25 April 2023
  • 2 replies
  • 116 views

Userlevel 2
Badge +1

Hello!

 

I want to know how to avoid setting key values inside this file using env vars.

 

{ "default": { "heapAutoInit": true },
"dev": { "heapAutoInit": true, "heapAppId": "YOUR_DEV_APP_ID" },
"prod": { "heapAutoInit": true, "heapAppId": "YOUR_PROD_APP_ID" } }

 

What I tried is to convert the json file as a js file exporting de object

And then importing the react-native-config library, it didn’t thrown an error but didn’t track anything :

 

import Config from ‘react-native-config’

 

export default{ "default":
{ "heapAutoInit": true },
"dev": { "heapAutoInit": true, "heapAppId": Config.HEAP_DEV },
"prod": { "heapAutoInit": true, "heapAppId": Config.HEAP_PROD } }

 

There is a better approach for that?

 

Looking forward to hearing from you soon.

 

Thanks

icon

Best answer by ALabs I Bhupender 27 April 2023, 09:57

View original

2 replies

Userlevel 3
Badge +1

Hi,

The approach you have taken is correct, however, if you are not seeing any tracking data, it might be worth checking if the environment variables are correctly set up and are being read by the app. You can verify this by logging the values of Config.HEAP_DEV and Config.HEAP_PROD to the console and see if they are returning the correct values.

Another approach you could take is to use a package like “dotenv” which allows you to load environment variables from a “.env” file. This way, you can store your API keys in a separate file and load them using “dotenv” in your code.

Create a “.env” file in the root directory of your project and add the API keys.

HEAP_DEV=your_dev_api_key
HEAP_PROD=your_prod_api_key
import dotenv from 'dotenv';
dotenv.config();
export default {
"default": { "heapAutoInit": true },
"dev": { "heapAutoInit": true, "heapAppId": process.env.HEAP_DEV },
"prod": { "heapAutoInit": true, "heapAppId": process.env.HEAP_PROD }
}

This way, you can keep your API keys separate from your codebase and load them dynamically at runtime.

Hope this helps! Do let me know if it works for you.😁

Userlevel 2
Badge +1

Hi @ALabs I Bhupender!

Thanks for your answer.

In my case, I don’t want to install another library, I am working with react-native-config.

What I did and it works is the manual Initialization. 

export default{

"default": {

"heapAutoInit": true

},

"dev": {

"heapAutoInit": true,

},

"prod": {

"heapAutoInit": true,

}

}

I deleted the app ids from heap.config.json file and I added only one env var inside app.js to set the app id.

Heap.setAppId(Config.HEAP_APP_ID);

Also some Android configurations are required in the documentation for Manual Initialization :

https://developers.heap.io/docs/react-native

I’ve added a buildConfigType to support env vars inside Android:

I hope this would be useful to another one with the same doubt.

Thanks!

Reply