So I finished this Google Apps integration with Magento. It was an interesting and challenging job (interesting and challenging usually go hand in hand) and my expectations on using the Apps Script API were different than how it turned out, for better or worse.

Bit of background, the store I worked on sells heavily customisable products - predominantly in the B2B space. Rather than forcing customers to enter their specifications through the web store, customers are shared in a Google Spreadsheet which they can fill out to communicate the requirements.

Google provides various API’s to create Google Drive documents, and the main one is the Google Apps Script API. I also considered the Spreadsheet API, but decided it’s not as powerful (and actually I don’t think there’s any point in ever using it - correct me if I’m wrong).

Apps Script is basically Javascript that runs on Google’s environment by an authenticated and authorized user.

When programming with Apps Script, you’ve got access to a bunch of global variables to interact with Google Services. For example, below code will get the Gmail drafts of the user that runs the code:

var drafts = GmailApp.getDraftMessages();

Apps Script can run in two contexts, as a standalone application, or bound to a container. When ran in a standalone fashion, an Apps Script project is just a file in someone’s Google Drive that’s either triggered manually, or by a service called “Google Apps Script Execution API”.

A diagram of the system looks something like this:

Diagram of Magento and Google Apps Script integration

A Magento admin user authenticates with Google using OAuth2 and authorizes some permissions. The refresh and access tokens get saved in Magento’s system config, since there’s only one authenticated user per website.

Then, after order placement, the Apps Script project gets executed, which eventually creates the document, and shares in the appropriate parties.

So far so good.

I also tried hooking up the deployment of the Apps Script project with normal code deployments (I use Envoyer.io), but this failed miserably and I sort of gave up that idea - Apps Script projects being a file somebody's Google Drive makes it really hard to update.

Even if the deployments work, someone manually has to go into the Apps Script project, and deploy it as an API executable with a new version number.

Also, you can only deploy and execute Apps Script projects that are owned by you. This means that if person x maintains the application, but person y uses it, the Apps Script project has to be owned by y, and manually deployed by y. This can lead to somewhat awkward situations because sometimes the deployment of the Magento integration and the Apps Script project have to be in sync.

There were some other annoying bits; the official code examples not saving the refresh token, some scopes not working well with each other, depreciated scopes that are still being referenced by official documentation and vagueness on how to deal with development, testing, staging and production environments.

All things considered, the platform is massive and probably where the complexity comes from. Being a user of Google Apps for business myself though, I think it's pretty cool because it opens up tons of possibilities. I enjoyed working with it.. but just double your estimates ;)

apps script integrations magento