1. 程式人生 > >Protocols: APIs and Extensions Documentation

Protocols: APIs and Extensions Documentation

Protocols API

Protocols customers receive early access to Segment’s Platform API, which enables programmatic creation, configuration, and fetching of core Segment platform resources such as Sources, Destinations, and now Tracking Plans. The Platform API represents Segment’s commitment to developers, enabling customers to extend their workflows around customer data collection and activation. The Platform API will be generally available to customers in coming months and will be evolving with more functionality throughout next year.

Supported Operations

  • List Tracking Plans
  • Get Tracking Plan
  • Create Tracking Plan
  • Update Tracking Plan
  • Delete Tracking Plan

Comprehensive documentation with Guides, Tutorials, and References for the Platform API is available to Protocols customers in our Developer Portal, which is currently private. For access, just

get in touch!

Debug Endpoint

This endpoint enables customers to send sample .track(), .identify(), .group(), .page() and .screen() requests to a debug endpoint that will return an error response if that payload is invalid. The event payload will not be delivered to the Segment Source or any active Destinations. Customers can use this endpoint in testing suites or to test payloads against current Schema filters or a Tracking Plan spec. Follow the instructions below to test sample payloads without delivering the event to Segment or downstream Destinations.

Authentication: This endpoint uses the same Authentication protocol outlined in our HTTP docs.

Enable debug mode with Analytics.js

The following snippet can be added to your dev environment or executed in a web console to use the debug endpoint. When enabled, all outbound Segment events will hit the debug endpoint. Events will not be delivered to Segment Destinations, so make sure to disable this when deploying your code to production.

analytics.Integrations["Segment.io"].prototype._enqueue = analytics.Integrations["Segment.io"].prototype.enqueue;
analytics.Integrations["Segment.io"].prototype.enqueue = function(path, msg, fn) {
  this.options.apiHost = 'debug-api.segment.com/v1'
  return this._enqueue(path, msg, fn)
};

Enable debug mode with querystring flag

Analytics.js does not have a built-in ‘debug mode’ flag yet. You can add a querystring flag to your Segment instrumentation with the following snippet:

// Point to the debug-api when the URL contains the query param "?segment_debug=true"

var apiHost;
if (window.location.href.indexOf('segment_debug=true') !== -1) {
  apiHost = 'debug-api.segment.com/v1';
} else {
  apiHost = 'api.segment.io/v1';
}

analytics.load("YOUR_WRITE_KEY", {
  integrations: {'Segment.io': { apiHost: apiHost } }
});

NOTE: Make sure this is only used in development environments since the debug-api does not send data downstream!

Error responses

The debug endpoint API will return detailed errors depending on the violation generated.

Error ResponseDescription
Invalid JSONThe JSON payload is invalid. Check to make sure your payload contains valid JSON.
Invalid writeKeySegment source writekey is not valid. Check your source settings.
Missing writeKeySegment source writekey is missing from payload. Make sure writekey is included in request.
Missing event key for track callThe payload is missing track call name. Make sure your payload includes "``event``"``:``"``My Event Name``".
Event must be a stringThe payload has an event key, but the value is not a string. Make sure the value associated with the event key is a string.
Missing userId or anonymousIdEvery Segment event must contain either a userId, anonymousId or both. Make sure to include 1 or both IDs in your payload.
context integrations must be an objectWhen specifying event context or integrations, they must be passed in an object. Make sure the value associated with the context or integrations key is an object.
Disabled eventThe event has either been disabled in Schema or is not included in your Tracking Plan. If you expect this event to be enabled, check your Source schema tab to see if the event is disabled, or add it to the Tracking Plan associated to the Source.
properties.Required: properties.Required is requiredThe event is missing a required property defined in the Tracking Plan. If the event does not require the property, update the Tracking Plan associated to the source. Otherwise, update the request payload.
properties.Optional: Invalid type. Expected: string, given: arrayThe event property is passing as an array, but expects a string as defined in the Tracking Plan. If the event property should be an array, update the Tracking Plan associated to the source. Otherwise, update the request payload.

End to End API Example

NOTE: These APIs are beta, which means that their names and functionality might change. If you find any bugs or have any feedback, please contact us.

Create a Personal Access Token

Programmatic access to the Segment Platform API for resources that you own happens through personal access tokens. Personal access tokens belong to Segment users, and can be used to access resources owned by the user who created them.

To set up Segment Protocols through the API you first need to create a personal access token with full access to your workspace through the workspace scope. Use your Segment account email and password:

$ [email protected]
$ PASS=<redacted>

$ curl \
  -d '{"access_token": {"description": "my access token", "scopes": "workspace", "workspace_names": ["workspaces/example"]}}' \
  -u "$USER:$PASS" \
  https://platform.segmentapis.com/v1alpha/access-tokens

Example response:

{
  "name": "access-tokens/5",
  "description": "my access token",
  "scopes": "workspace",
  "workspace_names": ["workspaces/example"],
  "create_time": "2018-10-12T22:36:39Z",
  "token": "qiTgISif4zprgBb_5j4hXfp3qhDbxrntWwwOaHgAMr8.gg9ok4Bk7sWlP67rFyXeH3ABBsXyWqNuoXbXZPv1y2g"
}

NOTE: You can not retrive the plain-text token after this so write it down or store it programmatically.

Create a Source

The Platform API enables you to create event sources, each of which has a corresponding unique “write key” that you configure the Segment SDK with.

Let’s create an event source to see how the Tracking Plan works on data we send to it:

$ ACCESS_TOKEN=<ACCESS-TOKEN-VALUE>

$ curl \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -d '{"source": {"name": "workspaces/example/sources/js", "catalog_name": "catalog/sources/javascript"}}' \
  https://platform.segmentapis.com/v1alpha/workspaces/example/sources

Example response:

{
 "name": "workspaces/example/sources/js",
 "parent": "workspaces/example",
 "catalog_name": "catalog/sources/javascript",
 "write_keys": [
  "LxKXARX9IuqR9v0gn7y2Fw1iHi0ephbF"
 ],
 "create_time": "2018-09-12T21:15:54.169Z",
 "library_source_config": {}
}

For good measure, let’s send a couple of events to our new Source. Note that event and userID are required. See the Tracking API spec for more details.

$ WRITE_KEY=LxKXARX9IuqR9v0gn7y2Fw1iHi0ephbF

$ curl https://api.segment.io/v1/track \
  -u $WRITE_KEY: \
  -H 'Content-type: application/json' \
  -d '{
    "event": "User Logged In",
    "userId": "78e56a08-ad10-42b1-88d2-b823623ac875",
    "properties": {
      "username": "user1"
    }
  }'

$ curl https://api.segment.io/v1/track \
  -u $WRITE_KEY: \
  -H 'Content-type: application/json' \
  -d '{
    "event": "User Login",
    "userId": "45677435-e419-475D-a884-c0a01bd36619",
    "properties": {
      "userName": "user2"
    }
  }'

To verify your events were sent to the Source, open the event debugger in your browser, e.g. https://app.segment.com/example/sources/js/debugger.

Create a Tracking Plan

Now we can configure a Tracking Plan on the workspace. Here we define that we only expect a single event User Logged In with a required username property. This will help us find and eliminate events with subtle differences like the name User Login or properties like userName, resulting in pristine data in Segment and all Destinations.

$ curl \
    -H "Authorization: Bearer $ACCESS_TOKEN" \
    -d '{
      "tracking_plan": {
        "display_name": "My Tracking Plan",
        "rules": {
          "events": [
            {
              "rules": {
              "$schema": "http://json-schema.org/draft-04/schema#",
              "type": "object",
              "properties": {
                "properties": {
                  "type": "object",
                  "properties": {
                    "username": {
                    "description": "",
                    "id": "/properties/properties/properties/username",
                    "type": "string"
                    }
                  },
                  "required": [
                    "username"
                  ]
                }
              },
              "required": [
                "properties"
              ]
              },
              "name": "User Logged In",
              "description": "Sent when a user logs into the web app"
            }
          ]
        }
      }
    }' \
    https://platform.segmentapis.com/v1beta/workspaces/example/tracking-plans

Example response:

{
 "name": "workspaces/example/tracking-plans/rs_1AAPTLy8ffAAQth0PvL8rOvU6PJ",
 "display_name": "My Tracking Plan",
 "rules": {
  "events": [
   {
    "name": "User Logged In",
    "description": "Sent when a user logs into the web app",
    "rules": {
     "properties": {
      "properties": {
       "properties": {
        "username": {
         "type": "string",
         "description": "",
         "id": "/properties/properties/properties/username"
        }
       },
       "required": [
        "username"
       ],
       "type": "object"
      }
     },
     "required": [
      "properties"
     ],
     "$schema": "http://json-schema.org/draft-04/schema#",
     "type": "object"
    }
   }
  ]
 },
 "create_time": "2018-09-13T19:20:37Z",
 "update_time": "2018-09-13T19:20:37Z"
}

Update a Tracking Plan

Over time the Tracking Plan will evolve and include more events. We can update the plan to include another event User Created with a required username property:

$ curl \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -X PUT \
  -d '{
    "update_mask": {
      "paths": [
        "tracking_plan.display_name",
        "tracking_plan.rules"
      ]
    },
    "tracking_plan": {
      "display_name": "My Updated Tracking Plan",
      "rules": {
        "events": [
          {
            "rules": {
              "$schema": "http://json-schema.org/draft-04/schema#",
              "type": "object",
              "properties": {
                "properties": {
                  "type": "object",
                  "properties": {
                    "username": {
                      "description": "",
                      "id": "/properties/properties/properties/username",
                      "type": "string"
                    }
                  },
                  "required": [
                    "username"
                  ]
                }
              },
              "required": [
                "properties"
              ]
            },
            "name": "User Logged In",
            "description": "Sent when a user logs into the web app"
          },
          {
            "rules": {
              "$schema": "http://json-schema.org/draft-04/schema#",
              "type": "object",
              "properties": {
                "properties": {
                  "type": "object",
                  "properties": {
                    "username": {
                      "description": "",
                      "id": "/properties/properties/properties/username",
                      "type": "string"
                    }
                  },
                  "required": [
                    "username"
                  ]
                }
              },
              "required": [
                "properties"
              ]
            },
            "name": "User Created",
            "description": "Sent when a user is created in the web app"
          }
        ]
      }
    }
  }' \
  https://platform.segmentapis.com/v1beta/workspaces/example/tracking-plans/rs_1AAPTLy8ffAAQth0PvL8rOvU6PJ
{
  "name": "workspaces/example/tracking-plans/rs_1AqjFLhQGTrpI7k3cipnSVF71n2",
  "display_name": "My Updated Tracking Plan",
  "rules": {
    "events": [
      {
        "name": "User Logged In",
        "description": "Sent when a user logs into the web app",
        "rules": {
          "required": [
            "properties"
          ],
          "$schema": "http://json-schema.org/draft-04/schema#",
          "type": "object",
          "properties": {
            "properties": {
              "type": "object",
              "properties": {
                "username": {
                  "description": "",
                  "id": "/properties/properties/properties/username",
                  "type": "string"
                }
              },
              "required": [
                "username"
              ]
            }
          }
        }
      },
      {
        "name": "User Signed Up",
        "description": "Sent when a user signs up for the web app",
        "rules": {
          "$schema": "http://json-schema.org/draft-04/schema#",
          "type": "object",
          "properties": {
            "properties": {
              "required": [
                "username"
              ],
              "type": "object",
              "properties": {
                "username": {
                  "type": "string",
                  "description": "",
                  "id": "/properties/properties/properties/username"
                }
              }
            }
          },
          "required": [
            "properties"
          ]
        }
      }
    ]
  },
  "create_time": "2018-09-28T18:55:19Z",
  "update_time": "2018-09-28T18:58:03Z"
}

Connect Tracking Plan to Source and Start Blocking Non-Conforming Data

Now we can review the Tracking Plan and connect it to a Source.

Click on “My Tracking Plan” to open the editor and review the events.

Click on “…” to connect the Tracking Plan to the js source.

Now open the Source Schema in your browser, e.g. https://app.segment.com/example/sources/js/schema2/events. We can see the that one event we sent conforms to the plan, and the other does not.

Now we can go all-in on the Tracking Plan, and block events that don’t conform from being sent to Destinations. Click “Edit” next to “Unplanned Data”, and select “Block” for unplanned track calls.

From here, we can also configure a Source to receive violation notifications

You can verify how the Tracking Plan works by sending events to the debug-api.segment.com endpoint.

We’ll use the write keys returned from the creation operation against the Sources API as the password to the debug tracking API.

Example command:

$ curl https://debug-api.segment.com/v1/track \
  -u $WRITE_KEY: \
  -H 'Content-type: application/json' \
  -d '{
    "event": "User Logged In",
    "userId": "78e56a08-ad10-42b1-88d2-b823623ac875",
    "properties": {
      "username": "user1"
    }
  }'

Example response:

{
  "success": true
}

Example command:

$ curl https://debug-api.segment.com/v1/track \
  -u $WRITE_KEY: \
  -H 'Content-type: application/json' \
  -d '{
    "event": "User Logged In",
    "userId": "78e56a08-ad10-42b1-88d2-b823623ac875",
    "properties": {
      "userName": "user1"
    }
  }'

Example response:

{
  "success": false,
  "message": "properties.username: properties.username is required"
}

Example command:

$ curl https://debug-api.segment.com/v1/track \
  -u $WRITE_KEY: \
  -H 'Content-type: application/json' \
  -d '{
    "event": "User Login",
    "userId": "45677435-e419-475D-a884-c0a01bd36619",
    "properties": {
      "userName": "user2"
    }
  }'

Example response:

{
  "success": false,
  "message": "Disabled event"
}

Google Sheets Tracking Plan Uploader

Thousands of Segment customers have used Google Sheets to build Tracking Plans. We created the following template to help you draft a Tracking Plan and easily upload that Tracking Plan to Segment. Keep in mind that uploading changes from Google Sheets will overwrite any changes made within the Segment UI.

To upload your Tracking Plan directly from Google Sheets, follow these steps:

  1. Generate an App following the steps here
  2. Copy your App Client ID and Secret and your tracking plan’s rs_ id (found in the URL path of your Tracking Plan) to the Importer Settings worksheet. Then click on the Segment > Send to Segment menu item to upload your Tracking Plan to Segment.

If you have any questions, or see anywhere we can improve our documentation, please let us know!

相關推薦

Protocols: APIs and Extensions Documentation

Protocols APIProtocols customers receive early access to Segment’s Platform API, which enables programmatic creation, configuration, and fetching of core S

JSON APIs and Ajax

posit 就是 禁止 wal ready getjson 鍵值 current 信息 1. 通過jQuery來綁定點擊事件。 函數 $(document).ready()這個函數中的代碼只會在我們的頁面加載時候運行一次,確保執行js之前頁面所有的dom已經準備就緒。 在$

Microsoft Dynamics CRM 2015 and Microsoft Dynamics CRM 2016 Performance and Scalability Documentation

摘要: 本人微信公眾號:微軟動態CRM專家羅勇 ,回覆285或者20181126可方便獲取本文,同時可以在第一間得到我釋出的最新博文資訊,follow me!我的網站是 www.luoyong.me 。 Microsoft Dynamics CRM 2015 and Microsoft Dynamics C

Predictive APIs and Apps Conference ← Inductio Ex Machina ← Mark Reid

Predictive APIs and Apps Conference I’m very happy to be involved with this year’s Predictive APIs and Apps conference, which is held in Sydney this yea

Upcoming Conference in Sydney on Predictive APIs and Apps ← Inductio Ex Machina ← Mark Reid

Upcoming Conference in Sydney on Predictive APIs and Apps After a big kick-off in Barcelona last year with over 200 attendees, the second International

How do I decide between Redshift, Postgres, and BigQuery? Documentation

Comparing Redshift and PostgresIn most cases, you will get a much better price-to-performance ratio with Redshift for typical analyses.Redshift lacks some

[Nuxt] Load Data from APIs with Nuxt and Vuex

his pro -- http template map https etc not run In a server-rendered application, if you attempt to load data before the page renders and

[Nuxt] Use Vuex Actions to Delete Data from APIs in Nuxt and Vue.js

export begin async delet tin remove todo ras alt You‘ll begin to notice as you build out your actions in Vuex, many of them will look qui

[ Git篇 ] git push / Please read the documentation and contact an administrator

更新程式碼的時候出現: 具體操作步驟: git remote update git rebase origin name 出現如下: Counting objects: 8, done. Delta compression using up to 4

5 - Relationships and hyperlinked APIs

教程 5:關係和超連結 API 目前我們的 API 中的關係是通過使用主鍵來表示。在本教程的這一部分中,我們將改進 API 的內聚力和可發現性,而不是使用超連結來進行關係。 為我們的 API 的根地址建立端點 現在我們有 ‘snippets’ 和 ‘users’ 端點,但我們沒有

TensorFlow Low-Level-APIs Graphs and Sessions學習筆記

Graphs and Sessions 本文翻譯自tensorflow官方網站的教程,只作為個人學習筆記,請勿用作商業用途。 tf使用一個數據流圖去表示使用Operation構建起來的計算流程,在low level api上需要和計算圖打交道,定義圖然後使用

TensorFlow Low-Level-APIs Save and Restore學習筆記

Save and Restore 本文翻譯自tensorflow官方網站的教程,只作為個人學習筆記,請勿用作商業用途。 tf.train.Saver類提供了儲存和提取模型的方法。tf.saved_model.simple_save函式也是一種簡單的方法來儲存模型

【Benson的專欄】Learning, staying up to date, and working on the latest and greatest in languages and APIs is what k

Learning, staying up to date, and working on the latest and greatest in languages and APIs is what k

Building and Documenting Python REST APIs With Flask and Connexion

In Part 1 of this series, you used Flask and Connexion to create a REST API providing CRUD operations to a simple in-memory structure called PEOPLE. Tha

The importance and impact of APIs in Serverless

When talking about serverless, two advantages immediately pop up:Scaling out of the box — Probably the best thing we can think of as developers.Don’t manag

How to use APIs with Pandas and store the results in Redshift

How to use APIs with Pandas and store the results in RedshiftHere is an easy tutorial to help understand how you can use Pandas to get data from a RESTFUL

Swift Course: Extensions & Protocols

Method RequirementsProtocols can require specific instance methods and type methods to be implemented by conforming types. These methods are written as par

The Future of Drag and Drop APIs

The Future of Drag and Drop APIsFront-end development matures. We used to keep state in global variables and DOM. Then we wired our apps with message buses

Kotlin for Android (IV): Custom Views and Android Extensions

After reading what can do for you, you might be wondering what´s next. As we talked in , this language makes Android development much simpler and th