Skip to content

Commit

Permalink
Opt-in new endpoint (#230)
Browse files Browse the repository at this point in the history
* opt-in new endpoint

* notes
  • Loading branch information
imnutz authored Jun 21, 2022
1 parent ffb5996 commit 21ed120
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 33 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

[![CircleCI](https://circleci.com/gh/treasure-data/td-js-sdk/tree/master.svg?style=svg)](https://circleci.com/gh/treasure-data/td-js-sdk/tree/master)

> :warning: NOTE: From version 3.1.0, we use our new JavaScript endpoint to log data, the `host` configuration
need to be changed to point to the new endpoint, see [Streaming Ingestion](STREAMING_INGESTION.md) for more information
> :warning: NOTE: In version 3.1, we support our new JavaScript endpoint to log data, however there are configurations need to be
changed in order to opt-in this feature, see [Streaming Ingestion](STREAMING_INGESTION.md) for more information

# Table of Contents
[Getting started](#getting-started)
Expand Down
6 changes: 5 additions & 1 deletion STREAMING_INGESTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

## Configurations

We still use the same configurations, but the `host` configuration needs to be changed so that it will point to our new endpoint.
We introduce new option to opt-in our new JavaScript endpoint, named `useNewJavaScriptEndpoint`, which has value of `true` or `false`.
When you enable this option, you need to change the `host` configuration as well, so that it will point to our new endpoint

:information_source: This new feature won't impact the server side cookie and the personalization features

The `host` configuration will have the following values, depending on which environment you want to ingest data.

Expand All @@ -24,6 +27,7 @@ Example:
var foo = new Treasure({
database: 'foo',
writeKey: 'your_write_only_key',
useNewJavaScriptEndpoint: true,
host: 'us01.records.in.treasuredata.com'
});
```
2 changes: 1 addition & 1 deletion bin/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
VERSION=$(cat $ROOT_DIR/package.json | jq -r '.version')
HOST='in.treasuredata.com'
DATABASE=""
PATHNAME="/"
PATHNAME="/js/v3/event/"
GLOBAL="Treasure"
FILENAME="td"
TO_VERSION=$(echo $VERSION | sed 's/\.[-a-zA-Z0-9]*$//g')
Expand Down
2 changes: 1 addition & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ module.exports = {
VERSION: '3.1.0',
HOST: 'in.treasuredata.com',
DATABASE: '',
PATHNAME: '/'
PATHNAME: '/js/v3/event/'
}
5 changes: 5 additions & 0 deletions lib/configurator.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var defaultSSCCookieDomain = function () {
exports.DEFAULT_CONFIG = {
database: config.DATABASE,
development: false,
useNewJavaScriptEndpoint: false,
globalIdCookie: '_td_global',
host: config.HOST,
logging: true,
Expand Down Expand Up @@ -86,6 +87,10 @@ exports.configure = function configure (options) {

validateOptions(this.client)

if (this.client.useNewJavaScriptEndpoint) {
this.client.pathname = '/'
}

if (!this.client.endpoint) {
this.client.endpoint = 'https://' + this.client.host + this.client.pathname
}
Expand Down
27 changes: 19 additions & 8 deletions lib/plugins/globalid.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ function fetchGlobalID (success, error, forceFetch, options) {
if (!this.inSignedMode()) {
return error('not in signed in mode')
}

if (!this.isGlobalIdEnabled()) {
return error('global id is not enabled')
}
var cookieName = this.client.globalIdCookie
var cachedGlobalId = cookie.getItem(cookieName)
if (cachedGlobalId && !forceFetch) {
Expand All @@ -89,17 +93,24 @@ function fetchGlobalID (success, error, forceFetch, options) {
options.sameSite = 'None'
}

var url = 'https://' + this.client.host
var url = 'https://' + this.client.host + '/js/v3/enable_global_id'
var requestHeaders = {}
var ignoreDefaultHeaders = false

if (this.client.useNewJavaScriptEndpoint) {
url = 'https://' + this.client.host

requestHeaders['Authorization'] = 'TD1 ' + this.client.writeKey
requestHeaders['User-Agent'] = navigator.userAgent
requestHeaders['Content-Type'] = misc.globalIdAdlHeaders['Content-Type']
requestHeaders['Accept'] = misc.globalIdAdlHeaders['Accept']
ignoreDefaultHeaders = true
}

api.get(url,
{
headers: {
'Authorization': 'TD1 ' + this.client.writeKey,
'User-Agent': navigator.userAgent,
'Content-Type': misc.globalIdContentTypeHeader,
'Accept': misc.globalIdAcceptHeader

}
headers: requestHeaders,
ignoreDefaultHeaders: ignoreDefaultHeaders
})
.then(function (res) {
var cachedId = cacheSuccess(res, cookieName, options)
Expand Down
46 changes: 30 additions & 16 deletions lib/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,29 +192,44 @@ exports._sendRecord = function _sendRecord (request, success, error, blockedEven
var url = request.url + '?' + params.join('&')
var isClickedLink = request.record.tag === 'a' && !!request.record.href

var authHeader = {
'Authorization': 'TD1 ' + request.apikey,
'User-Agent': navigator.userAgent
}

if (this.isGlobalIdEnabled()) {
authHeader['Content-Type'] = misc.globalIdContentTypeHeader
authHeader['Accept'] = misc.globalIdAcceptHeader
var requestHeaders = {}
var payload
var ignoreDefaultHeaders = false

if (this.client.useNewJavaScriptEndpoint) {
requestHeaders['Authorization'] = 'TD1 ' + request.apikey
requestHeaders['User-Agent'] = navigator.userAgent

if (this.isGlobalIdEnabled()) {
requestHeaders['Content-Type'] = misc.globalIdAdlHeaders['Content-Type']
requestHeaders['Accept'] = misc.globalIdAdlHeaders['Accept']
} else {
requestHeaders['Content-Type'] = misc.adlHeaders['Content-Type']
requestHeaders['Accept'] = misc.adlHeaders['Accept']
}

ignoreDefaultHeaders = true

payload = {
events: [request.record]
}
} else {
requestHeaders['X-TD-Write-Key'] = request.apikey
payload = JSON.stringify(request.record)
}

if (window.fetch && (this._windowBeingUnloaded || isClickedLink)) {
api
.postWithTimeout(
url,
{
events: [request.record]
},
payload,
this.client.jsonpTimeout,
{
method: 'POST',
keepalive: true,
credentials: 'include',
headers: authHeader
ignoreDefaultHeaders: ignoreDefaultHeaders,
headers: requestHeaders
}
)
.then(success)
Expand All @@ -223,11 +238,10 @@ exports._sendRecord = function _sendRecord (request, success, error, blockedEven
api
.post(
url,
payload,
{
events: [request.record]
},
{
headers: authHeader
ignoreDefaultHeaders: ignoreDefaultHeaders,
headers: requestHeaders
}
)
.then(success)
Expand Down
14 changes: 12 additions & 2 deletions lib/utils/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,22 @@ function camelCase (str) {
}, '')
}

var adlHeaders = {
'Content-Type': 'application/vnd.treasuredata.v1+json',
'Accept': 'application/vnd.treasuredata.v1+json'
}

var globalIdAdlHeaders = {
'Content-Type': 'application/vnd.treasuredata.v1.js+json',
'Accept': 'application/vnd.treasuredata.v1.js+json'
}

module.exports = {
disposable: disposable,
invariant: invariant,
fetchWithTimeout: fetchWithTimeout,
camelCase: camelCase,
isLocalStorageAccessible: isLocalStorageAccessible,
globalIdContentTypeHeader: 'application/vnd.treasuredata.v1.js+json',
globalIdAcceptHeader: 'application/vnd.treasuredata.v1.js+json'
adlHeaders: adlHeaders,
globalIdAdlHeaders: globalIdAdlHeaders
}
9 changes: 7 additions & 2 deletions lib/utils/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ var OK_STATUS = 200
var NOT_MODIFIED = 304

var defaultHeaders = {
'Content-Type': 'application/vnd.treasuredata.v1+json',
'Accept': 'application/vnd.treasuredata.v1+json'
'Content-Type': 'application/json',
'X-TD-Fetch-Api': 'true'
}

var FETCH_CREDENTIALS = {
Expand Down Expand Up @@ -149,6 +149,11 @@ function _timeout (milliseconds, promise, timeoutMessage) {
function postWithTimeout (url, body, milliseconds, options) {
if (window.AbortController) {
var controller = new window.AbortController()

var headers = getHeaders(options.headers, isDefaultHeadersIgnored(options))

options.headers = headers

var promise = window.fetch(url, Object.assign({}, options, {signal: controller.signal}))
var timeoutId = setTimeout(function () {
controller.abort()
Expand Down

0 comments on commit 21ed120

Please sign in to comment.