Reference Material
====

Thanks to this local post https://salesforce.stackexchange.com/questions/415087/adding-a-crm-analytics-dashboard-to-a-salesforce-page-so-users-without-license-c, that contains this insight that sums up the process well:

> It's all super convoluted, I am afraid

I've found this [Tableau CRM Packaging for ISVs](https://medium.com/inside-the-salesforce-ecosystem/tableau-crm-packaging-isvs-559c4220efaf) Medium post (also [posted on Salesforce](https://appexchange.salesforce.com/learn/tableau-crm-packaging-for-isvs)). It's full of magical JSON edits and includes the key point:

> However, the best and the only recommended way to distribute a package
> is by choosing the “Analytics Template” component.

There is this [Salesforce documentation](https://developer.salesforce.com/docs/atlas.en-us.bi_dev_guide_wave_templates.meta/bi_dev_guide_wave_templates/bi_templatesdev_app_type.htm):

> An embedded app template has a templateType of embeddedapp. An
> embedded app doesn’t require users to have access to Analytics Studio.
> The embedded app is created when a managed package containing the
> template and other supporting assets is installed in a Salesforce org.
> An embedded template uses the auto-install framework to install the
> app in an org without user interaction.


Note this valuable (150 pages though - yikes) reference information to help with the hand-editing of the template [Analytics Templates Developer
Guide](https://resources.docs.salesforce.com/latest/latest/en-us/sfdc/pdf/bi_dev_guide_wave_templates.pdf).

But a key source for me is answers here https://salesforce.stackexchange.com/questions/419478/comprehensive-documentation-on-creating-embedded-app-template-for-crm-analytics. Particularly how to work with scratch orgs where:

 - Adding the feature "DevelopmentWave" turn on CRM Analytics - you can do the development in this org.
 - Adding the feature "AnalyticsAppEmbedded" (and not adding "DevelopmentWave") makes the "Analytics View Only User" permission set available - use this org to test your package Analytics Template.

For production there is a  $0 "Analytics View Only Embedded App" SKU that needs to be turned on to get the equivalent of the "AnalyticsAppEmbedded" scratch org feature.

Checklist
====

This is how to package CRM Analytics that can be installed in an org that doesn't have full CRM Analytics licenses.

- You'll need to convert your Analytics components to an Analytics template; the metadata type for that is [WaveTemplateBundle](https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_wavetemplatebundle.htm) and the files are put in a `waveTemplates` folder
- Manual edits are needed, especially `auto-install.json`, `"templateType" : "embeddedapp"` and `"shares": [ { "shareType": "Organization", "accessType": "View" } ]`; as the install is not interactive, no need/possibility to create input field pages
- I removed the `wave` folder to package as I got a "dashboard with devname [xxxxxxxx_tp] already exists" error on install otherwise
- When the package is installed/upgraded, the results of the Analytics App install are shown in "Auto-Installed Apps" in "Setup" together with some log information (but a successful install doesn't mean users will be able to see the analytics; both the `"shares":` and the following point matter AFAIK)
- Assign the "Analytics View Only User" permission set to any user that needs to see the analytics

This can all be tested in scratch orgs, but for production the "Analytics View Only Embedded App" SKU must be added.