JavaScript API + Server Side API Integration

If you are using both the Javascript API and one of the Server Side APIs, ensure that both APIs use the same identity when recording events for one person. Doing so will keep your data consistent, regardless of which API provided the event/property.

If you want to get the identity that the Javascript API has assigned your visitor, you can inspect two cookie values that KISSmetrics sets:

Then you can call alias using the km_ai value and whatever identity you are using server-side.

Alternatively, the JavaScript function KM.i() will also return the visitor’s KISSmetrics ID, in case you need to pass that along to another function.

Since the KISSmetrics script loads asynchronously, you’ll want to make sure that our script has loaded before you try to call KM.i(). To ensure that is the case, you can wrap your call to KM.i() within a function, then push that function onto the _kmq array. Once the KM script has loaded, it will go through the _kmq array, executing your function.

<script type="text/javascript">
_kmq.push(function()
  { alert(KM.i()) } // Display an alert box with your current KM identity
);
</script>

Although you could set these cookies server-side we do not recommend doing this. If you want to set the identity on the server-side the best thing to do is to render some Javascript code that calls identity on the next page view with your named identity:

_kmq.push(['identify', <%= @identity.to_json %>])

Filed under Advanced

Search


Learn more about KISSmetrics

Get started now