Can I use the JavaScript Library on multiple domains?
- Several Subdomains
- Several Top-Level Domains
- What do I need to do?
- What are the tradeoffs for tracking each domain separately vs. under one API key?
The JavaScript library keeps track of each user’s ID as a first party cookie. This is usually fine, but if your customers navigate to two or more domains while using your site, that means there will be two or more cookies that represent a person, and you should ensure that both cookies use the same value to represent that person.
Several Subdomains
Let’s say you want to track what happens as people browse on different subdomains of your site, and you want to see it all in the same Kissmetrics site.
- http://www.mysite.com
- http://blog.mysite.com
- http://help.mysite.com
- https://secure.mysite.com
Notice that all of these are part of .mysite.com
.
If you have put the same JavaScript code on all of these subdomains, by default we will be able to remember it is the same visitor as they browse from one subdomain to the next. That way you can set up to record events from each subdomain, and we can show whether it’s the same person going through these subdomains.
Note: if you are using the Event Wizard to set up events, please make sure this JavaScript setting is enabled. This was enabled by default for all accounts created after October 2012:
To Track Subdomains Separately…
If you want to track the subdomains separately in two Kissmetrics sites, you can specify a cookie domain KM_COOKIE_DOMAIN
before the Kissmetrics Javascript is included. This might look like:
Several Top-Level Domains
However, let’s say you are tracking two different domains:
- http://www.mysite.com
- http://www.myblog.com
This is a little more difficult, because now each domain has its own set of cookies to keep track of each visitor.
What do I need to do?
So far, we’ve seen two methods that work:
- Maintain their ID server-side. This is most useful when the user can sign in to both domains, typically if you have a Single Sign-On system. That lets you identify a logged-in customer with the same username/email address/database ID, regardless of which domain they’re on.
- When someone leaves one domain to go to another, append the destination URL with our URL API, which lets you pass their KM ID from the first domain to the next. This way lets you pass our anonymous IDs from a domain to the next. Be sure that the same Kissmetrics code block is on both domain’s webpages.
Let’s look at an example of #2:
- Your site is on
www.yoursite.com
, but you useyoursite.3rd-party-checkout.com
as part of a funnel you are tracking. - There is a link that takes people from
www.yoursite.com
toyoursite.3rd-party-checkout.com
. - Adjust the link, using our URL API to tag the current KM ID as part of the destination URL.
Example 1: Known Users
This is what hard-coding a URL would look like, to identify anyone who clicks this as “bob@bob.com”:
yoursite.3rd-party-domain.com/?kmi=bob%40bob.com
Example 2: Anonymous Users
You can use the KM.i()
function to obtain the visitor’s current ID (anonymous or not), and make use of that. Here’s a JavaScript function that you can use to create tagged URLs to enable cross domain tracking.
Down the line, if the customer ever goes from being anonymous to providing some identifying info, you’ll have to alias the anonymized and known id’s together, rather than rely on identify
to do the job. (The URL API treats the transferred ID as a “known” ID.)
For reference: Looking Up Your Current KM ID
What are the tradeoffs for tracking each domain separately vs. under one API key?
-
If you track your different domains separately, as individual sites within your account:
- (+) It’s easier to see each domain in isolation.
- (+) You do not have to worry about making sure each person’s identity stays the same across your domains.
- (-) However, it’s much harder to compare activity across the domains, because of how we keep the data separated.
-
If you track your different domains as one site within your account:
- (+) You can see a complete picture of people’s activity from your marketing site to your app or checkout cart.
- (+) You can still create separate events to differentiate reporting on activity in one domain vs. the other.
- (-) However, there is extra setup to ensure that people are being represented with the same ID on all of your tracked domains.
- (-) If you end up using the same JS snippet on all of your domains, the events our JavaScript automatically tracks would lump together activity from each of your domains. Visited Site and Ad Campaign Hit would include Visits and Ad hits to all of your domains.