Kraken Data was approached by a client to provide a plan for integrating Webtrends Analytics into an Apple TV application.
This appeared to be a simple request given our experience integrating with iOS and Android apps. Having never developed an Apple TV app before our first step was to build a basic tvOS application to understand how all the screens and actions are handled, and then tried to incorporate the Webtrends iOS SDK. And this is when things started to get interesting.What we found out was:
- tvOS Apps can be built using JavaScript and TVML instead of writing all your code in Objective C or Swift.
- Most code examples for tvOS are in Swift and we have mostly coded in Objective C.
- The Webtrends SDK was not able to be installed within a tvOS App.
Now that we knew the challenges it was time to put together a solution. Learning Swift has been on the agenda for a while so this would be a good opportunity to do that. Webtrends have an API which will allow us to POST events back to their servers, so we will try and use that.
Building a basic tvOS app was extremely quick and easy with the Xcode tvOS template and utilising JavaScript and TVML files. Once this basic app was built it was clear that we would need a solution which would allow us to post event data from within the JavaScript application file. We built a basic JavaScript function utilising XMLHttpRequest which allowed us to post data to Webtrends and called this function every time a screen changed. This was great, we now knew that we could post data from a tvOS application and it would be received by Webtrends. What we didn’t know was how the client was planning to build their app. Were they planning on using pure Swift, or utilising JavaScript and TVML. They didn’t know either but thought it would probably be built in Swift. We suspected that their development team may have never created a tvOS app before and would likely utilise JavaScript and TVML once they found out how simple it was for building apps. So, we now needed to develop a solution which would work with Swift and from within JavaScript.
The Solution
Without knowing which way our client was going to build the app we decided it would be best to write a set of functions in Swift and provide a way for JavaScript to access these functions. This would provide the ability to send events to Webtrends Analytics from within a JavaScript file or from Swift code. To ensure all events are attributed to the same visitor, we included functions to request Unique Visitor IDs from the Webtrends API and then stored these IDs within the NSUserDefaults. All this happens in the background without our client having to be concerned with managing Visitor IDs.
Now all our client needs to do is:
- Add their Webtrends client DCS ID to their apps plist file.
- Include our Webtrends Analytics functions Swift file into their project.
- Call the function wt_postEvents() or wt_postEventsJS() with any parameters which require passing through to Webtrends.