danwilson / google-analytics-plugin

Cordova (PhoneGap) Plugin to connect to the native Google's Universal Analytics SDK 3.0
MIT License
693 stars 500 forks source link

Configuring play-services version on capacitor #588

Open p-v-d-Veeken opened 3 years ago

p-v-d-Veeken commented 3 years ago

In the documentation it says: _Use the GMSVERSION to align the required play-services version with other plugins.

cordova plugin add cordova-plugin-google-analytics --variable GMS_VERSION=11.0.1

However I'm working on a capacitor project and although it was fairly painless to install the plugin, I'm now having issues building the app due to version mismatches:

Error: All gms/firebase libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 17.0.0, 11.0.1. Examples include com.google.android.gms:play-services-base:17.0.0 and com.google.android.gms:play-services-analytics-impl:11.0.1 

It appears this problem could be trivially fixed by specifying the GMS_VERSION, but I don't know if that's possible with capacitor. Any help would be greatly appreciated. :)

p-v-d-Veeken commented 3 years ago

For anyone having the same problem, the following hack solved it for me:

  1. Adding the following to your app's package.json
    "scripts": {
        "postinstall": "npx ts-node scripts/setGoogleAnalyticsVersion.ts"
    }
  2. Create scripts/setGoogleAnalyticsVersion.ts and add the following contents:
    
    import * as fs from 'fs';

const GMS_VERSION = '17.0.0'; const PLUGIN_PATH = './node_modules/cordova-plugin-google-analytics/plugin.xml';

console.log(Attempting to set $GMS_VERSION = ${GMS_VERSION} in ${PLUGIN_PATH});

const pluginContents = fs.readFileSync(PLUGIN_PATH).toString('utf8'); const pluginContentsUpdated = pluginContents.replace('$GMS_VERSION', GMS_VERSION);

fs.writeFileSync(PLUGIN_PATH, pluginContentsUpdated);

console.log('Success!');



No doubt there are better ways to achieve the same thing, but at least it works. 
victorsosa commented 2 years ago

PR are welcome