Last Updated: February 25, 2016
·
790
· adtaylor

ProMotion with Google Analytics

Today I needed to add the Google Analytics SDK into a ProMotion project and ran into an issue where I needed to extend the View Controller from the Google ViewController rather than the ProMotion Screen. This wasn't going to happen.

Instead I built a little helper to add some methods to ProMotions screens. This is far from perfect but this is something we needed to add in quickly and it does the job right now.

The Helper


module ProMotion
  module ScreenModule

      def tracker
        GAI.sharedInstance.defaultTracker
      end

      def build_event(category, action, label=nil, value=nil)
        event = GAIDictionaryBuilder.createEventWithCategory category,
                                                              action: action,
                                                              label: label,
                                                              value: value
        event.build
      end

      def send_ga_event(category, action, label=nil, value=nil)
        event = build_event(category, action, label, value)
        tracker.send event
      end

      def screenName=(name)
        tracker.set "&cd", value: name
        appView = GAIDictionaryBuilder.createAppView 
        tracker.send appView.build
      end

  end
end

How to use

It offers 2 very basic pieces of functionality; sending the screen name and events.

To send the screen name just set the screenName variable.

class HomeScreen < PM::Screen

  ...

  def on_appear
    self.screenName = "Home Screen"
  end

  ...

end

and to send an event from your view controller you can use the send_ga_event method.

send_ga_event  [category], [action], [label], [value]
send_ga_event "ui_action", "button_click", item.name, item.id

Like I mentioned, this is very simplistic but I hope it gets someone else out of a bind if you hit the same issue.