Skip to content

Commit 5f19989

Browse files
jooohhnkelsonpw
andauthored
docs: Add options.js to jsdoc and reference website (#327)
* clean and reorganize website scripts * Add jsdoc to options.js * Add Options.md to sidebar Co-authored-by: Kelson Warner <[email protected]>
1 parent a01cc19 commit 5f19989

File tree

7 files changed

+100
-18
lines changed

7 files changed

+100
-18
lines changed

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,9 @@
6565
"test": "make test",
6666
"dev": "node test/browser/server.js",
6767
"docs:install": "cd website/ && yarn install",
68-
"docs:generate-jsdoc": "node website/generate-jsdoc",
68+
"docs:generate-jsdoc": "cd website && yarn generate-jsdoc",
6969
"docs:start": "cd website/ && yarn start",
70-
"docs:build": "cd website/ && yarn build",
71-
"docs:serve": "cd website/ && yarn serve",
72-
"docs:deploy": "cd website/ && yarn deploy",
73-
"docs:swizzle": "cd website/ && yarn swizzle"
70+
"docs:deploy": "cd website/ && yarn deploy"
7471
},
7572
"bugs": {
7673
"url": "https://github.com/amplitude/amplitude-javascript/issues"

src/amplitude-client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ AmplitudeClient.prototype.Revenue = Revenue;
7474
* @param {string} apiKey - The API key for your app.
7575
* @param {string} opt_userId - (optional) An identifier for this user.
7676
* @param {object} opt_config - (optional) Configuration options.
77-
* See [options.js](https://github.com/amplitude/Amplitude-JavaScript/blob/master/src/options.js#L14) for list of options and default values.
77+
* See [options.js](https://amplitude.github.io/Amplitude-JavaScript/Options) for a list of options and default values.
7878
* @param {function} opt_callback - (optional) Provide a callback function to run after initialization is complete.
7979
* @example amplitudeClient.init('API_KEY', 'USER_ID', {includeReferrer: true, includeUtm: true}, function() { alert('init complete'); });
8080
*/

src/options.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,43 @@ if (BUILD_COMPAT_REACT_NATIVE) {
1111
}
1212
}
1313

14+
/**
15+
* Options used when initializing Amplitude
16+
* @typedef {Object} Options
17+
* @property {string} [apiEndpoint=`api.amplitude.com`] - Endpoint to send amplitude event requests to.
18+
* @property {boolean} [batchEvents=`false`] - If `true`, then events are batched together and uploaded only when the number of unsent events is greater than or equal to eventUploadThreshold or after eventUploadPeriodMillis milliseconds have passed since the first unsent event was logged.
19+
* @property {number} [cookieExpiration=`365`] - The number of days after which the Amplitude cookie will expire. 12 months is for GDPR compliance.
20+
* @property {string} [cookieName=`amplitude_id`] - *DEPRECATED*
21+
* @property {string} [sameSiteCookie='None'] - Sets the SameSite flag on the amplitude cookie. Decides cookie privacy policy.
22+
* @property {boolean} [cookieForceUpgrade=`false`] - Forces pre-v6.0.0 instances to adopt post-v6.0.0 compat cookie formats.
23+
* @property {boolean} [deferInitialization=`null`] - If `true`, disables the core functionality of the sdk, including saving a cookie and all logging, until explicitly enabled. To enable tracking, please call `amplitude.getInstance().enableTracking()` *Note: This will not affect users who already have an amplitude cookie. The decision to track events is determined by whether or not a user has an amplitude analytics cookie. If the `cookieExpiration</code> is manually defined to be a short lifespan, you may need to run `amplitude.getInstance().enableTracking()` upon the cookie expiring or upon logging in.*
24+
* @property {boolean} [disableCookies=`false`] - Disable Ampllitude cookies altogether.
25+
* @property {string} [deviceId=A randomly generated UUID.] - The custom Device ID to set. *Note: This is not recommended unless you know what you are doing (e.g. you have your own system for tracking user devices).*
26+
* @property {boolean} [deviceIdFromUrlParam=`false`] - If `true`, then the SDK will parse Device ID values from the URL parameter amp_device_id if available. Device IDs defined in the configuration options during init will take priority over Device IDs from URL parameters.
27+
* @property {string} [domain=The top domain of the current page's URL. ('https://amplitude.com')] - Set a custom domain for the Amplitude cookie. To include subdomains, add a preceding period, eg: `.amplitude.com`.
28+
* @property {number} [eventUploadPeriodMillis=`30000` (30 sec)] - Amount of time in milliseconds that the SDK waits before uploading events if batchEvents is true.
29+
* @property {number} [eventUploadThreshold=`30`] - Minimum number of events to batch together per request if batchEvents is true.
30+
* @property {boolean} [forceHttps=`true`] - If `true`, the events will always be uploaded to HTTPS endpoint. Otherwise, it will use the embedding site's protocol.
31+
* @property {boolean} [includeGclid=`false`] - If `true`, captures the gclid URL parameter as well as the user's initial_gclid via a setOnce operation.
32+
* @property {boolean} [includeReferrer=`false`] - If `true`, captures the referrer and referring_domain for each session, as well as the user's initial_referrer and initial_referring_domain via a setOnce operation.
33+
* @property {boolean} [includeUtm=`false`] - If `true`, finds UTM parameters in the query string or the _utmz cookie, parses, and includes them as user properties on all events uploaded. This also captures initial UTM parameters for each session via a setOnce operation.
34+
* @property {string} [language=The language determined by the browser] - Custom language to set.
35+
* @property {string} [logLevel=`WARN`] - Level of logs to be printed in the developer console. Valid values are 'DISABLE', 'ERROR', 'WARN', 'INFO'. To learn more about the different options, see below.
36+
* @property {boolean} [logAttributionCapturedEvent=`false`] - If `true`, the SDK will log an Amplitude event anytime new attribution values are captured from the user. **Note: These events count towards your event volume.** Event name being logged: [Amplitude] Attribution Captured. Event Properties that can be logged: `utm_source`, `utm_medium`, `utm_campaign`, `utm_term`, `utm_content`, `referrer`, `referring_domain`, `gclid`. For UTM properties to be logged, `includeUtm` must be set to `true`. For the `referrer` and `referring_domain` properties to be logged, `includeReferrer` must be set to `true`. For the `gclid` property to be logged, `includeGclid` must be set to `true`.
37+
* @property {boolean} [optOut=`false`] - Whether or not to disable tracking for the current user.
38+
* @property {function} [onError=`() => {}`] - Function to call on error.
39+
* @property {string} [platform=`Web`|`iOS`|`Android`] - Platform device is running on. `Web` is a browser (including mobile browsers). `iOS` and `Android` are relevant only for react-native apps.
40+
* @property {number} [savedMaxCount=`1000`] - Maximum number of events to save in localStorage. If more events are logged while offline, then old events are removed.
41+
* @property {boolean} [saveEvents=`true`] - If `true`, saves events to localStorage and removes them upon successful upload. *Note: Without saving events, events may be lost if the user navigates to another page before the events are uploaded.*
42+
* @property {boolean} [saveParamsReferrerOncePerSession=`true`] - If `true`, then includeGclid, includeReferrer, and includeUtm will only track their respective properties once per session. New values that come in during the middle of the user's session will be ignored. Set to false to always capture new values.
43+
* @property {boolean} [secureCookie=`false`] - If `true`, the amplitude cookie will be set with the Secure flag.
44+
* @property {number} [sessionTimeout=`30*60*1000` (30 min)] - The time between logged events before a new session starts in milliseconds.
45+
* @property {Object} [trackingOptions=`{ city: true, country: true, carrier: true, device_manufacturer: true, device_model: true, dma: true, ip_address: true, language: true, os_name: true, os_version: true, platform: true, region: true, version_name: true}`] - Type of data associated with a user.
46+
* @property {boolean} [unsetParamsReferrerOnNewSession=`false`] - If `false`, the existing `referrer` and `utm_parameter` values will be carried through each new session. If set to `true`, the `referrer` and `utm_parameter` user properties, which include `referrer`, `utm_source`, `utm_medium`, `utm_campaign`, `utm_term`, and `utm_content`, will be set to `null` upon instantiating a new session. Note: This only works if includeReferrer or includeUtm includeUtm are set to `true`.
47+
* @property {string} [unsentKey=`amplitude_unsent`] - localStorage key that stores unsent events.
48+
* @property {string} [unsentIdentifyKey=`amplitude_unsent_identify`] - localStorage key that stores unsent identifies.
49+
* @property {number} [uploadBatchSize=`100`] - The maximum number of events to send to the server per request.
50+
*/
1451
export default {
1552
apiEndpoint: 'api.amplitude.com',
1653
batchEvents: false,

website/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ docs/Amplitude.md
1313
docs/AmplitudeClient.md
1414
docs/Identify.md
1515
docs/Revenue.md
16+
docs/Options.md
1617

1718
# Misc
1819
.DS_Store

website/generate-jsdoc.js

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,32 @@ const jsdoc2md = require("jsdoc-to-markdown");
22
const fs = require("fs");
33
const path = require("path");
44
const prettier = require("prettier");
5-
const publicFiles = [
5+
const publicClassFiles = [
66
"amplitude-client.js",
77
"amplitude.js",
88
"identify.js",
99
"revenue.js",
1010
];
11+
const publicTypedefFiles = ["options.js"];
1112
const srcDir = path.join(__dirname, "../", "src");
1213
const outputDir = path.join(__dirname, "docs");
13-
function generateMarkdown(inputFile) {
14+
15+
function generateTypedefMarkdown(inputFile) {
16+
const inputFilePath = path.join(srcDir, inputFile);
17+
const data = jsdoc2md.getTemplateDataSync({ files: inputFilePath });
18+
const name = data.find((e) => e.kind === "typedef").name;
19+
const filteredData = data.filter((e) => e.kind === "typedef");
20+
const outputFilePath = path.join(outputDir, `${name}.md`);
21+
const markdownOutput = filteredData
22+
.map((item) => documentOptionsFile(item))
23+
.join("\n");
24+
fs.writeFileSync(
25+
path.join(outputDir, `${name}.md`),
26+
prettier.format(markdownOutput, { parser: "mdx" })
27+
);
28+
}
29+
30+
function generateClassMarkdown(inputFile) {
1431
const inputFilePath = path.join(srcDir, inputFile);
1532
const data = jsdoc2md.getTemplateDataSync({ files: inputFilePath });
1633
const className = data.find((e) => e.kind === "class").name;
@@ -22,15 +39,38 @@ function generateMarkdown(inputFile) {
2239
const outputFilePath = path.join(outputDir, `${className}.md`);
2340

2441
const markdownOutput = filteredData
25-
.map((item) => documentItem(item))
42+
.map((item) => documentClassFile(item))
2643
.join("\n");
2744
fs.writeFileSync(
2845
path.join(outputDir, `${className}.md`),
2946
prettier.format(markdownOutput, { parser: "mdx" })
3047
);
3148
}
3249

33-
function documentItem(data) {
50+
function documentOptionsFile(data) {
51+
return `${documentHeader(data)}
52+
53+
${data.description}
54+
55+
Option | Type | Description | Default
56+
-------|------|-------------|---------
57+
${documentOptionsProperties(data)}
58+
59+
`;
60+
}
61+
62+
function documentOptionsProperties(data) {
63+
return data.properties
64+
.map(
65+
(prop) =>
66+
`${prop.name || ""} | ${data.properties[0].type.names.join("|")} | ${
67+
prop.defaultvalue || ""
68+
} | ${prop.description || ""}`
69+
)
70+
.join("\n");
71+
}
72+
73+
function documentClassFile(data) {
3474
return `${documentHeader(data)}
3575
3676
${data.examples ? documentExamples(data) : ""}
@@ -46,9 +86,8 @@ ${data.returns ? documentReturn(data) : ""}
4686
}
4787

4888
function documentHeader(data) {
49-
if (data.deprecated)
50-
return `## ~~\`${data.id}\`~~`
51-
return `## \`${data.id}\``
89+
if (data.deprecated) return `## ~~\`${data.id}\`~~`;
90+
return `## \`${data.id}\``;
5291
}
5392

5493
function documentExamples(data) {
@@ -67,7 +106,7 @@ function documentDeprecated(data) {
67106

68107
function documentParams(data) {
69108
const params = data.params.map(
70-
(param) => `- \`${param.name}\` (\`${param.type.names.join('|')}\`)
109+
(param) => `- \`${param.name}\` (\`${param.type.names.join("|")}\`)
71110
${param.description}
72111
`
73112
);
@@ -78,15 +117,22 @@ ${params.join("\n")}
78117

79118
function documentReturn(data) {
80119
return `### Return Value
81-
- (\`${data.returns[0].type.names.join('|')}\`)
120+
- (\`${data.returns[0].type.names.join("|")}\`)
82121
${data.returns[0].description}
83122
`;
84123
}
85124

125+
// Main Script
126+
86127
if (!fs.existsSync(outputDir)) {
87128
fs.mkdirSync(outputDir);
88129
}
89130

90-
for (const file of publicFiles) {
91-
generateMarkdown(file);
131+
for (const file of publicClassFiles) {
132+
generateClassMarkdown(file);
92133
}
134+
135+
for (const file of publicTypedefFiles) {
136+
generateTypedefMarkdown(file);
137+
}
138+

website/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"scripts": {
66
"docusaurus": "docusaurus",
7+
"generate-jsdoc": "node generate-jsdoc",
78
"start": "docusaurus start",
89
"build": "docusaurus build",
910
"swizzle": "docusaurus swizzle",

website/sidebars.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
22
sidebar: {
3-
'API Reference': ['AmplitudeClient', 'Amplitude', 'Identify', 'Revenue'],
3+
'API Reference': ['AmplitudeClient', 'Amplitude', 'Identify', 'Revenue', 'Options'],
44
},
55
};

0 commit comments

Comments
 (0)