- request for creating(updating) a project(per store) in karte
- embed js code in html header
[APP_PUBLIC_KEY]
&[APP_PRIVATE_KEY]
would be sent from karte.io per app.
request(get) format:
https://karte.io/asp/api/0.1/create?key=[APP_PUBLIC_KEY]&email=store@example.com&store_id=store_id&time=[UNIX TIMESTAMP]&enable=true&signature=[SIGNATURE]
signature
is a HMAC token with sha256. How to create asignature
param is described in the sample code.- for updating
enable
parameter, you should changecreate
toupdate
in request url.
response format:
{
"message": "an account was created successfully.",
"email": "[email protected]",
"store_id": "store_id",
"enable": False,
"api_key": "[STORE_API_KEY]"
}
sample code by python:
import unittest
from time import time
import hmac
import hashlib
import urllib2
import json
class TestKarteAspApi(unittest.TestCase):
"""
setup
"""
def setUp(self):
self.url_base = "https://karte.io/asp/api/0.1/"
self.private_key = "[APP_PRIVATE_KEY]"
self.public_key = "[APP_PUBLIC_KEY]"
"""
util function: create hmac signature by sha256
"""
def create_signature(self, key, email, store_id, time, enable, private_key):
data = "key=%s&email=%s&store_id=%s&time=%d&enable=%s" % (key, email, store_id, time, "true" if enable else "false")
sig = hmac.new(private_key, data, hashlib.sha256).hexdigest()
return sig
def test_karte_api(self):
print 'test karte api start --------'
# url ---------------------
url = self.url_base + "create"
# for updating `enable` parameter, only changing the url is needed.
# url = self.url_base + "update"
# parameters ---------------------
key = self.public_key
email = "[email protected]"
store_id = "store_id"
t = int(time())
enable = False
sig = self.create_signature(key, email, store_id, t, enable, self.private_key)
# encode params
email = urllib2.quote(email)
# get ---------------------
data = "?key=%s&email=%s&store_id=%s&time=%d&enable=%s&signature=%s" % (key, email, store_id, t, "true" if enable else "false", sig)
try:
res = json.loads(urllib2.urlopen(url + data).read())
except urllib2.URLError, e:
print "error code = %s" % e.code # != 200
raise e
else:
# == 200
print "get 200"
# assertion ---------------------
self.assertIsNotNone(res['api_key'])
# App must store this 'api_key' per store to embed js in header.
The below code must be embeded in all html header where you would like to send any data.
[STORE_API_KEY]
is the token which you get in chapter 1.
<script type="text/javascript">
!function(){var a;a=a||[],function(){var b,c,d,e;for(c=["identify","track"],b=function(b){return function(){return a.push([b].concat(Array.prototype.slice.call(arguments,0)))}},d=0,e=[];d<c.length;)a[c[d]]=b(c[d]),e.push(d++);return e}(),a.init=function(b,c){var d,e;return a.api_key=b,a.options=c||{},a.options.tracking_url="https://karte.io",e=document.createElement("script"),e.type="text/javascript",e.async=!0,e.src="https://karte.io/libs/tracker.js",d=document.getElementsByTagName("script")[0],d.parentNode.insertBefore(e,d)},a.Date=function(){function a(a){this.name="trackerDate",this.source=a}return a}(),window.tracker=a}.call(this);
tracker.init("[STORE_API_KEY]");
</script>
The code should be embeded in a html header if user is logging in.
For example you should put it in a login page or an account setting page.
<script type="text/javascript">
tracker.identify({
// 1. required -----------------------
user_id: "user_unique_id",
// 2. recommended -----------------------
email: "[email protected]",
subscription: false,
name: 'kobayashi hiroki',
create_date: new Date("2012/12/01"),
// 3. options -----------------------
// conversion stats
buy_times: 10,
buy_total: 100,
last_buy_date: new Date("2012/12/01"),
// you can send any data like below
point: 100,
gender: 'F',
birth: new Date('1982/09/19'),
tel: '000000000',
zip: "000",
country: "japan",
pref: "tokyo",
address: "shibuya shoto 2"
});
</script>
You should embed conversion code in a thanks page.
<script type="text/javascript">
var items = [
{
// required -----------------------
item_id: "item_unique_id",
name: "item name",
price: 100,
quantity: 1,
// recommended -----------------------
category: ["category1", "category2"]
}
];
tracker.track('buy', {
// required -----------------------
transaction_id: "transaction_unique_id",
revenue: 1000,
items: items,
// recommended -----------------------
shipping: 100,
tax: 10
});
</script>
You can send any custom events like below.
<script type="text/javascript">
tracker.track('upgrade_plan', {
plan: 'gold'
});
</script>
tracker.js
uses component as a module loader.I thought that It wouldn't conflict with other module loaders, but the error seems to be caused by the conflict.
I'm sorry I didn't check the conflict with other loaders.
I will check it today. Please wait a while..