Menu
 

Send data using the Javascript Snippet

Our basic integration uses a javascript snippet like many other analytics tools. This is a low-impact way for you to send data to Customer.io.

When you create an account, we’ll create your unique site id for the javascript snippet. You should then include the snippet just before the end of the body tag on every page in your app.

Here’s what the snippet looks like:

<script type="text/javascript">
  var _cio = _cio || [];

  (function() {
    var a,b,c;a=function(f){return function(){_cio.push([f].
    concat(Array.prototype.slice.call(arguments,0)))}};b=["identify",
    "track"];for(c=0;c<b.length;c++){_cio[b[c]]=a(b[c])};
    var t = document.createElement('script'),
        s = document.getElementsByTagName('script')[0];
    t.async = true;
    t.id    = 'cio-tracker';
    t.setAttribute('data-site-id', 'YOUR_SITE_ID');
    t.src = 'https://assets.customer.io/assets/track.js';
    s.parentNode.insertBefore(t, s);
  })();
</script>

Next, you’ll need to identify your users to us. Add this identify function in your html, beneath the previous snippet.

<script type="text/javascript">
  // Only send this when a user is logged in
  _cio.identify({
  // Required attributes
  id: 'user_287',           // Unique id for the currently signed in user in your application.
  email: 'user@domain.com', // Email of the currently signed in user.
  created_at: 1339438758,   // Timestamp in your system that represents when
                            // the user first signed up. You'll want to send it
                            // as seconds since the epoch.

  // Optional (these are examples. You can name attributes what you wish)

  first_name: 'John',       // Add any attributes you'd like to use in the email subject or body.
  plan_name: 'premium'      // To use the example segments, set this to 'free' or 'premium'.
  });
</script>

We require that you send id (a unique user id), email and created_at. You can also send us any addtional user attributes that are of value to you. Here we are sending the optional attributes first_name and plan_name. You can use these attributes in the body of an email, or you can use them to segment your users.

Important things to know

  • We only track activity after a user logs in. We do not track anonymous user activity.

  • To update a user’s attributes, just send the “identify” call again with the same id and the new attribute values.

  • A javascript snippet makes it really easy to get started, but isn’t essential. You may do everything through our REST API