Creating Your own Android Notification for Piwik

Creating Your own Android Notification for Piwik

Update (9/24/14): Piwik had a new release today (ver 2.7.0) and that release introduced a new field the CSV file being pulled down from the API. That new field wound up being a breaking change to this profile, but it’s an easy fix. I’ve updated the two XML files (Profile_Piwik.xml and Profile_AN_LaunchPiwik.xml) with those updates. However, I did not update the screenshots I took for this blog originally. The %Piwik2, %Piwik3, %Piwik9 and %Piwik10 Tasker variables all had to be incremented by one to account for the new field. Same with the %PiwikYest Tasker variables. It seems to be working well for me after these changes.

Also, somewhere since originally publishing this blog, I also tinkered with DashClock and added a lockscreen widget that shows the Piwik stats captured by tasker. That’s included in the updated XML files.

Shortly after standing up my blog, Pat and I started to become curious about web metrics. We were really motivated to capture and analyze the data of the dozens (on a good day) of people who were visiting our blogs on a daily basis. We used Google Analytics for quite some time, but noticed that page-generation times had trended up since enabling Google Analytics, and we thought we could improve that if our web metrics were hosted locally. Enter Piwik, a growing open-source web analytics platform. Pat dove right in, using Piwik from the start, and I lagged behind, first only using Google Analytics, then trying both before finally cutting over to Piwik full-time quite awhile ago.

At the time, our significant others were participating in some classes at the local recreation center. While they were out working hard, Pat and I would go out to eat, sporadically checking our Piwik statistics on the first smart phones over slow data networks. Back then, we wished for a mobile app (one exists now), and we wanted some sort of easy notification that we could see easily from our phones. To this day, a notification feature does not exist.

Ultimately, Pat took matters into his own hands. He used the Android app Tasker to create a custom Piwik Notification that did the trick. Essentially, what it did was use the Piwik API to pull down stats into a comma-separated values file on the phone. Tasker parsed those values into variables and then used some of those variables to create a persistent notification. When the latest versions of Piwik and Piwik Mobile came out, I wound up tweeting at Piwik, hoping to inspire them to build some notification features into the latest versions of Piwik Mobile:

Shortly after seeing and re-tweeting me, Pat came up with an excellent brainstorm. He wanted to include yesterday’s statistics within his existing notification, mostly because we’d wind up checking the notification right around midnight, trying to see the prior day’s total visitors and we looked too late and it’d already rolled over into the next day. Pat thought it’d be nice if the prior day’s totals were also part of the notification in case you viewed it too late. Furthermore, I thought it’d also be helpful to see throughout the day how the current day’s traffic stacks up to the prior day.

However, the notification you can create in Tasker is pretty basic. Pat and I both had already ran into limitations where our text was getting cut off in the Tasker notification. We just couldn’t find a way to cram all the information we wanted into that single notification, which seemed unfortunate, considering that most post-Jellybean notifications in Android could be pretty large and are expandable. You can read an entire email, you can read or reply to an SMS, and even share a photo to your favorite Social Media site all within these larger notifications. It seemed unfair we could not do the same within Tasker’s notification ability.

I set out to enhancing Pat’s Piwik notification, and following his good example, I wanted to share it just like he did. I did some research and found a Tasker plug-in called AutoNotification which boasted more powerful notifications for use in your Tasker profiles and tasks.

Before you get started, you’ll need the following Android apps. One caveat about the AutoNotification app: It works, but it’s pretty crippled unless you pay the $1.69 to unlock its full capabilities.

Next, you’ll want to download the following Tasker profiles, revise, and import them. You can read on the Tasker FAQ exactly how you can import the XML:

You’ll want to make changes to the XML in order to put your Piwik Server and authorization token into the HTTP get lines for both the Piwik Real-Time and Piwik Yesterday tasks (see below). You can do this from within Tasker itself, but I’d strongly recommend opening it up in a text editor and just editing the XML there. You’ll save yourself quite a bit of headache!

Piwik Yesterday /piwik/?module=API&idSite=3&date=yesterday&period=day&method=VisitsSummary.get&format=csv&token_auth=Your_Piwik_Token

Piwik Real-Time /piwik/?module=API&idSite=3&date=today&period=day&method=VisitsSummary.get&format=csv&token_auth=Your_Piwik_Token

You may also want to go through and do a find & replace on and replace it with whatever text you want. These are used in the notification title, ticker and other text fields. Again, you should do that within a text editor before you import it into Tasker.

Additionally, within each Task you should edit the path to the Piwik.csv and PiwikYest.csv files. On my Nexus 5, I’ve got them stored in their own folder named Piwik on the user’s storage (/storage/emulated/0/) because different devices have different storage, your actual path may be different than my own. The file does not have to exist for the task to succeed, but any folder it is being created in must exist first.

Essentially, what I did was add a step to pull down the stats for the prior day and parse them into variables onto Pat’s existing Tasker profile. I then replaced the existing notification functionality with the AutoNotification plug-in to create the notification. Because I wanted to launch the Piwik Mobile app when I touched the notification, I had to create a new Tasker Profile, AN: Launch Piwik. If you chose to not install Piwik Mobile or if you just don’t want that app to get launched each time you touch the notification, you’ll need to go in and delete the AN: LaunchPiwik profile in Tasker and then modify the notification and delete everything under Action.