Skip to content

Commit

Permalink
Merge pull request #5 from UVLabs/v120
Browse files Browse the repository at this point in the history
V120
  • Loading branch information
UVLabs authored Jul 30, 2023
2 parents d4c191e + d558810 commit 7a4e7dc
Show file tree
Hide file tree
Showing 17 changed files with 594 additions and 586 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MAUTICAPIUSER='<API_USER>'
MAUTICAPIPW='<API_PW>'
MAUTICAPIURL='<API_URL>'
19 changes: 0 additions & 19 deletions .gcloudignore

This file was deleted.

6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
vendor
vendor
.env
.DS_Store
log.txt
webhooks/*.json
80 changes: 38 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
# What it does 🤔

Coach Freem receives your webhooks from Freemius, dusts them off, gives them a pep talk, and then sends them off to Mautic (he doesn't know that he's an app).
Coach Freem receives your webhooks from Freemius, dusts them off, gives them a pep talk, and then sends them off to Mautic.

# Why 🤷

Because automation.

# How to use it 🛠️
In other words, Coach Freem receives webhooks from Freemius, saves them, processes them, and creates/updates the respect Mautic contact based on the Webhook received by Freemius.

Coach Freem was intended to be run as a Google Cloud Function that comes up every time the various webhooks are sent by Freemius. Google Cloud functions has a nice free tier so there's that.
When a webhook is received from Freemius, the contents are saved to the `webhooks` folder. This is so that the connection from Freemius can be disconnected so as to not timeout at 10 seconds, which is the maximum amount of time Freemius allows it's webhooks to run.

Coach isn't limited to Google Cloud Functions, he can easily be moved to your very own server with a few tweaks. All you'd need to do is create a route on your server/website that accepts POST requests, and pass that request body to Coach.
That pretty much happens on the first few lines of the `init` function. You can use a framework such as [SlimPHP](https://www.slimframework.com/) if you'd like to go that route.
A separate cron job then comes in to process the saved webhooks and creates/updates the mautic contact.

Coach makes use of Mautic's Basic Auth, so all you need to do is create an Admin user on your Mautic install and change the details in `setClient` method, inside `Client.php` in the `includes` folder. You also need to enable the REST API on your Mautic install.
# Why 🤷

By default the method expects environment variables for these values, but you can set the details manually for testing or actually set them inside your $PATH.
Because automation.

In production you'd set the set environment variables inside the Google Functions config.
# How to use it 🛠️

## Composer
Coach makes use of Mautic's Basic Auth, so all you need to do is:

On your localhost run `composer install` inside the root folder of Coach Freem.
## Google Functions Setup
- Create a custom field inside your Mautic install called `freemius_id`, make sure that the **Is Unique Identifier** switch for the field is set to **Yes**. Also make sure the alias is `freemius_id`

You may manually upload the source to the Google Cloud Functions dashboard but ideally you can make use of the Google Cloud SDK to deploy and redeploy new versions.
- Enable the Mautic RestAPI in its configuration settings

Install the Google Cloud sdk. If you're using MacOS then you can do so using [Homebrew](https://formulae.brew.sh/cask/google-cloud-sdk).
- Create an Admin *user* in Mautic

You can deploy the new version by running `composer deploy`. This will start the deploy steps and you may be asked for Google Cloud Credentials if you've never connected via command line before.
- Edit the `.env.example` file in the `coach-freem` folder then replace the placeholders with the user's credentials and the Mautic API URL (typically something like `https://<mautic-install-domain>/api/`

## Local Testing
- Rename `.env.example` to `.env`

You can start a local PHP server for testing your changes by running `composer start`, you will then be able to access Coach locally by going to `http://localhost:8080`. You can then `POST` Freemius webhooks to this URL using a tool like [Insomnia](https://insomnia.rest/) or [Postman](https://www.postman.com/).
## Functions to edit before going live

An example Freemius request webhook can be found in `docs/sample-freemius-webhook.json`, simply copy the contents of this file and paste it as a JSON POST request inside Insomnia/Postman to `http://localhost:8080`.
The following functions inside `index.php` should be edited to match your plugin and mautic details:

You can change the `type` key in the JSON request to the webhook you want to test.
- productIDs()
- customContactDataMappings()
- contactSegments()
- contactTags()
- excludedTLDs()
- isExcludedTLD()

For your own plugin/theme, you can use the Freemius webhook area to send all webhooks to a URL provided by https://webhook.site/ to examine the webhook content testing.
Comments have been added to help explain how these functions should be edited. If comments can be made better then feel free to submit a pull request!

## Freemius dashboard setup

Once you've deployed Coach Freem to Google Cloud Functions, you should get a URL where it can be accessed:
Once you've dropped Coach Freem to your server. You should add the following URL to your Freemius Custom Webhooks area: `https://example.com/coach-freem/?save=1`. Replace `example.com` with your domain name.

![Google Cloud Function URL](./docs/url.png)
The example URL assumes you have the `coach-freem` folder placed in the root directory of your domain.

Copy this URL and head to `Freemius Dashboard->Integrations->Custom Webhooks`, create a new webhook with the URL you copied before but for the event types **ONLY SELECT THE CURRENT EVENTS THAT ARE BEING CONSUMED BY COACH FREEM**. Selecting "all event types" would send every single Freemius event type to your Cloud Function causing unnecessary charges.
Copy this URL and head to `Freemius Dashboard->Integrations->Custom Webhooks`, create a new webhook with the URL you copied before but for the event types **ONLY SELECT THE CURRENT EVENTS THAT ARE BEING CONSUMED BY COACH FREEM**.

At the time of writing this line the only events being used by Coach Freem are:

Expand All @@ -58,32 +57,29 @@ At the time of writing this line the only events being used by Coach Freem are:
- install.deactivated
- install.uninstalled

You can confirm this by checking the `init()` function in `index.php`.
You can confirm this by checking the `process_webhook()` function in `index.php`.

Your webhook should look something like this:
## Cron Job Setup

![Example webhook configured](./docs/webhooks.png)
Add a cronjob that runs every 5 minutes to your server, the cronjob should hit the URL: `https://example.com/coach-freem/?process=1`. Replace `example.com` with your domain name.

In the image above you can see that we're sending all events to https://webhook.site, this is simply for testing purposes as mentioned above. You can turn off the webhook.site webhook once you're done testing.
## Functions to edit before going live
The example URL assumes you have the `coach-freem` folder placed in the root directory of your domain.

The following functions inside `index.php` should be edited to match your plugin and mautic details:
# Local Testing

- productIDs()
- customContactDataMappings()
- contactSegments()
- contactTags()
- excludedTLDs()
- isExcludedTLD()
You can start a local PHP server for testing your changes by running `php -S localhost:8080` in the root directory of Coach Freem. You will then be able to access Coach locally by going to `http://localhost:8080`. You can then `POST` Freemius webhooks to this URL using a tool like [Insomnia](https://insomnia.rest/) or [Postman](https://www.postman.com/).

Comments have been added to help explain how these functions should be edited. If comments can be made better then feel free to submit a pull request!
An example Freemius request webhook can be found in `docs/sample-freemius-webhook.json`, simply copy the contents of this file and paste it as a JSON POST request inside Insomnia/Postman to `http://localhost:8080`.

You can change the `type` key in the JSON request to the webhook you want to test.

For your own plugin/theme, you can use the Freemius webhook area to send all webhooks to a URL provided by https://webhook.site/ to examine the webhook content testing.


# What's missing 👨🏾‍💻

Well...I put this together to *just* get some automation set up for Mautic to further automate my email marketing (the whole reason I'm using it). So pull requests are welcome.
Well...I put this together to *just* get some automation set up for Mautic to further automate my email marketing (the whole reason I'm using it).

Missing:

- Handle when a user opts out of marketing.
- Ignore Local sites (create TLD blacklist like .local, .dev etc)
- Handle when a user opts out of marketing.
9 changes: 2 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"description": "Middleware between Freemius webhooks and Mautic API.",
"type": "library",
"require": {
"google/cloud-functions-framework": "^1.1",
"mautic/api-library": "^3.1"
"mautic/api-library": "^3.1",
"vlucas/phpdotenv": "^5.5"
},
"license": "GPLv2",
"authors": [
Expand All @@ -18,11 +18,6 @@
}
},
"scripts": {
"start": [
"Composer\\Config::disableProcessTimeout",
"FUNCTION_TARGET=init php -S localhost:${PORT:-8080} vendor/google/cloud-functions-framework/router.php"
],
"deploy": "gcloud functions deploy coach-freem --gen2 --project=freemius-related"
},
"require-dev": {
"spatie/ray": "^1.37"
Expand Down
Loading

0 comments on commit 7a4e7dc

Please sign in to comment.