forked from amplitude/Amplitude-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrevenue.js.html
205 lines (175 loc) · 6.76 KB
/
revenue.js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: revenue.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: revenue.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>var constants = require('./constants');
var type = require('./type');
var utils = require('./utils');
/*
* Wrapper for logging Revenue data. Revenue objects get passed to amplitude.logRevenueV2 to send to Amplitude servers.
* Note: price is the only required field. If quantity is not specified, then defaults to 1.
*/
/**
* Revenue API - instance constructor. Revenue objects are a wrapper for revenue data.
* Each method updates a revenue property in the Revenue object, and returns the same Revenue object,
* allowing you to chain multiple method calls together.
* Note: price is a required field to log revenue events.
* If quantity is not specified then defaults to 1.
* See [Readme]{@link https://github.com/amplitude/Amplitude-Javascript#tracking-revenue} for more information
* about logging Revenue.
* @constructor Revenue
* @public
* @example var revenue = new amplitude.Revenue();
*/
var Revenue = function Revenue() {
// required fields
this._price = null;
// optional fields
this._productId = null;
this._quantity = 1;
this._revenueType = null;
this._properties = null;
};
/**
* Set a value for the product identifer.
* @public
* @param {string} productId - The value for the product identifier. Empty and invalid strings are ignored.
* @return {Revenue} Returns the same Revenue object, allowing you to chain multiple method calls together.
* @example var revenue = new amplitude.Revenue().setProductId('productIdentifier').setPrice(10.99);
* amplitude.logRevenueV2(revenue);
*/
Revenue.prototype.setProductId = function setProductId(productId) {
if (type(productId) !== 'string') {
utils.log('Unsupported type for productId: ' + type(productId) + ', expecting string');
} else if (utils.isEmptyString(productId)) {
utils.log('Invalid empty productId');
} else {
this._productId = productId;
}
return this;
};
/**
* Set a value for the quantity. Note revenue amount is calculated as price * quantity.
* @public
* @param {number} quantity - Integer value for the quantity. If not set, quantity defaults to 1.
* @return {Revenue} Returns the same Revenue object, allowing you to chain multiple method calls together.
* @example var revenue = new amplitude.Revenue().setProductId('productIdentifier').setPrice(10.99).setQuantity(5);
* amplitude.logRevenueV2(revenue);
*/
Revenue.prototype.setQuantity = function setQuantity(quantity) {
if (type(quantity) !== 'number') {
utils.log('Unsupported type for quantity: ' + type(quantity) + ', expecting number');
} else {
this._quantity = parseInt(quantity);
}
return this;
};
/**
* Set a value for the price. This field is required for all revenue being logged.
* Note revenue amount is calculated as price * quantity.
* @public
* @param {number} price - Double value for the quantity.
* @return {Revenue} Returns the same Revenue object, allowing you to chain multiple method calls together.
* @example var revenue = new amplitude.Revenue().setProductId('productIdentifier').setPrice(10.99);
* amplitude.logRevenueV2(revenue);
*/
Revenue.prototype.setPrice = function setPrice(price) {
if (type(price) !== 'number') {
utils.log('Unsupported type for price: ' + type(price) + ', expecting number');
} else {
this._price = price;
}
return this;
};
/**
* Set a value for the revenueType (for example purchase, cost, tax, refund, etc).
* @public
* @param {string} revenueType - RevenueType to designate.
* @return {Revenue} Returns the same Revenue object, allowing you to chain multiple method calls together.
* @example var revenue = new amplitude.Revenue().setProductId('productIdentifier').setPrice(10.99).setRevenueType('purchase');
* amplitude.logRevenueV2(revenue);
*/
Revenue.prototype.setRevenueType = function setRevenueType(revenueType) {
if (type(revenueType) !== 'string') {
utils.log('Unsupported type for revenueType: ' + type(revenueType) + ', expecting string');
} else {
this._revenueType = revenueType;
}
return this;
};
/**
* Set event properties for the revenue event.
* @public
* @param {object} eventProperties - Revenue event properties to set.
* @return {Revenue} Returns the same Revenue object, allowing you to chain multiple method calls together.
* @example var event_properties = {'city': 'San Francisco'};
* var revenue = new amplitude.Revenue().setProductId('productIdentifier').setPrice(10.99).setEventProperties(event_properties);
* amplitude.logRevenueV2(revenue);
*/
Revenue.prototype.setEventProperties = function setEventProperties(eventProperties) {
if (type(eventProperties) !== 'object') {
utils.log('Unsupported type for eventProperties: ' + type(eventProperties) + ', expecting object');
} else {
this._properties = utils.validateProperties(eventProperties);
}
return this;
};
/**
* @private
*/
Revenue.prototype._isValidRevenue = function _isValidRevenue() {
if (type(this._price) !== 'number') {
utils.log('Invalid revenue, need to set price field');
return false;
}
return true;
};
/**
* @private
*/
Revenue.prototype._toJSONObject = function _toJSONObject() {
var obj = type(this._properties) === 'object' ? this._properties : {};
if (this._productId !== null) {
obj[constants.REVENUE_PRODUCT_ID] = this._productId;
}
if (this._quantity !== null) {
obj[constants.REVENUE_QUANTITY] = this._quantity;
}
if (this._price !== null) {
obj[constants.REVENUE_PRICE] = this._price;
}
if (this._revenueType !== null) {
obj[constants.REVENUE_REVENUE_TYPE] = this._revenueType;
}
return obj;
};
module.exports = Revenue;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude.html">Amplitude</a></li><li><a href="AmplitudeClient.html">AmplitudeClient</a></li><li><a href="Identify.html">Identify</a></li><li><a href="Revenue.html">Revenue</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Nov 08 2016 18:11:38 GMT-0800 (PST)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>