Google Analytics

OneSignal Features - Integrating OneSignal with Google Analytics

OneSignal supports tracking subscription and notification data when users subscribe and notifications are clicked on each platform, including sending this data to Google Analytics so that it can be analyzed in the context of your other user data.

📘

Checkout Our Webinar!

See the recorded session going over Google Analytics:
Google Analytics Webinar

Mobile GA Guide

See our Google Analytics for Firebase guide for more details.


Web Push GA Guide

Start with Adding Google Analytics.

Tracking Web Push Notification Clicks

By UTM Parameters

You can automatically add UTM parameters to push notification URLs through the OneSignal Dashboard.

The easiest way to track notification clicks is to add Google Analytics UTM parameters to the end of the URL for the notification. Here's an overview on UTM parameters: http://blog.rafflecopter.com/2014/04/utm-parameters-best-practices/

When sending a notification, please pick a utm_source (Such as "onesignal"), a utm_medium (Such as "web-push" or "api" vs "automated" vs "dashboard"), and a utm_campaign parameter (Such as "promotional-offer-template-123"). Next, add these parameters to your url, like so:

https://yoursite.com/your-page?utm_source=onesignal&utm_medium=web-push&utm_campaign=promotional-offer-template-123

After this, in Google analytics you can visit the Acquisition -> All Campaigns view to see all the campaigns you have sent and filter by day, browser, or campaign.

If you are having issues with UTM parameters, please see this article on common UTM parameter mistakes: https://penguininitiatives.com/common-utm-campaign-url-tracking-mistakes-to-avoid/

By Page JavaScript

You can use the addListenerForNotificationOpened event of the OneSignal Javascript SDK to detect when a user clicks a notification.

Add the Google Tracking code to the head tags of your site, then add this code to the body tags of the page you are directing users upon clicking the notification. This will track the notification ID and OneSignal player ID

<script>
OneSignal.push(["addListenerForNotificationOpened", function(payload) {
  console.log("OneSignal Notification Clicked Payload:");
  console.log(payload);
  OneSignal.getUserId( function(userId) {
    console.log("OneSignal User ID:", userId);
    // Make a POST call to GA with the notification data and userId aka playerId
    // https://developers.google.com/analytics/devguides/collection/analyticsjs/sending-hits#hitcallback
    ga('send', {
      hitType: 'event', 
      eventCategory: 'os_addListenerForNotificationOpened', 
      eventAction: 'u_id ' + userId, 
      eventLabel: 'n_id ' + payload.id,
      hitCallback: function() {
        console.log("ga os_addListenerForNotificationOpened callback");
      }
    });
  });
}]);
</script>

Tracking Subscriptions

Permission Prompt Change Events

You can use the notificationPermissionChange event of the OneSignal Javascript SDK to detect when a user subscribes to notifications or unsubscribes from notifications on your site.

Add the Google Tracking code to the head tags of your site, then add this code to the body tags of the pages users can subscribe. This will track the subscription change event and the OneSignal player ID.

OneSignal.push(function() {
    // Occurs when the user's subscription changes to a new value.
    OneSignal.on('notificationPermissionChange', function(permissionChange) {
      var currentPermission = permissionChange.to;
      console.log('New permission state:', currentPermission);
      OneSignal.getUserId( function(userId) {
        ga('send', {
          hitType: 'event',
          eventCategory: 'os_notificationPermissionChange',
          eventAction: 'u_id ' + userId,
          eventLabel: currentPermission, 
          hitCallback: function() {
            console.log("ga os_notificationPermissionChange callback");
          }
        });
      });
   });
});

This will create several event actions in Google Analytics when users opt-in or opt-out.
You can then use filtering options in Google Analytics to track by day, week, month, landing pages and browsers.

Tracking Impressions of the opt in request pop-up

You can use the permissionPromptDisplay method to send an event to Google Analytics from your page's code, like so:

OneSignal.push(function() {
  // Occurs when native browser prompt is shown
  OneSignal.on('permissionPromptDisplay', function() {
    console.log("The native prompt displayed");
    ga('send', {
      hitType: 'event',
      eventCategory: 'os_permissionPromptDisplay',
      eventAction: 'displayed',
      hitCallback: function() {
        console.log("ga os_permissionPromptDisplay callback");
      }
    });
  });
});

This will create an event in Google Analytics that you can track and filter by day, week, month, landing pages and browsers.

Tracking Actual Subscription Change

You can use the subscriptionChange event to track when specific users subscribe or unsubscribe from web push.

OneSignal.push(function() {
  // Occurs when the user's subscription changes to a new value.
  OneSignal.on('subscriptionChange', function (isSubscribed) {
    console.log("The user's subscription state is now:", isSubscribed);
    OneSignal.getUserId( function(userId) {
      ga('send', {
        hitType: 'event',
        eventCategory: 'os_subscriptionChange',
        eventAction: 'u_id ' + userId,
        eventLabel: isSubscribed,
        hitCallback: function() {
          console.log("ga os_subscriptionChange callback");
        }
      });
    })
  });
});

Tracking Notification Receipts & Dismissals

Advanced Topic

We recommend sending this data to Google Analytics as well (or another analytics tool). However since your website may not be open when notifications are received, dismissed, and opened by a user, this should be done by using our Webhook support.

For example, you can construct a webhook on top of the Google Analytics Measurement Protocol feature (https://developers.google.com/analytics/devguides/collection/protocol/v1/), then provide that URL with the query parameters of your choice when initializing OneSignal.

Filtering options like by day, week, month, browser, template will be available in Google analytics alongside the above event for your use.