-
Notifications
You must be signed in to change notification settings - Fork 5
/
usage_example.js
executable file
·131 lines (104 loc) · 3.91 KB
/
usage_example.js
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
#!/usr/bin/env node
var mParticle = require('./src/mparticle.js');
var batch = new mParticle.Batch(mParticle.Batch.Environment.development);
var appEvent = new mParticle.AppEvent();
appEvent.event_name = 'Test App Event';
appEvent.custom_event_type = 'other';
appEvent.custom_flags = {
foo: 'bar',
answer: 42,
arrays: ['foo', 'bar', 'baz']
};
var session_start = new mParticle.SessionStartEvent();
session_start.session_id = 12345678;
session_start.timestamp_unixtime_ms = 1552679728376;
batch.addEvent(session_start);
var user_identities = new mParticle.UserIdentities();
user_identities.customerid = '123456';
batch.user_identities = user_identities;
var device_info = new mParticle.DeviceInformation();
// set any IDs that you have for this user
device_info.ios_advertising_id = '07d2ebaa-e956-407e-a1e6-f05f871bf4e2';
device_info.android_advertising_id = 'a26f9736-c262-47ea-988b-0b0504cee874';
device_info.att_timestamp_unixtime_ms = 1552679728376;
device_info.att_authorization_status = mParticle.DeviceInformation.ATTAuthorizationStatus.authorized;
batch.device_info = device_info;
// arbitrary example allowing you to create a segment of users trial users
batch.user_attributes = { 'Account type': 'trial', TrialEndDate: '2016-12-01' };
var screen_view_event = new mParticle.ScreenViewEvent();
screen_view_event.screen_name = 'Test Screen View Event';
screen_view_event.custom_flags = {
foo: 'bar',
answer: 42,
arrays: ['foo', 'bar', 'baz']
};
var event = new mParticle.AppEvent(
mParticle.AppEvent.CustomEventType.navigation,
'Hello World'
);
batch.addEvent(event);
var product = new mParticle.Product();
product.name = 'Example Product';
product.id = 'sample-sku';
product.price = 19.99;
var product_action = new mParticle.ProductAction('purchase');
product_action.products = [product];
product_action.tax_amount = 1.5;
product_action.total_amount = 21.49;
var ccpa_consent_state = new mParticle.CCPAConsentState(
'document_agreement.v3',
true,
Date.now(),
'mparticle.test/signup',
'IDFA:3f90np3n-3108-nee2-90xd-a30bd9pb04pd'
);
var gdpr_consent_state = new mParticle.GDPRConsentState(
'document_agreement.v2',
true,
Date.now(),
'dtmgbank.com/signup',
'IDFA:a5d934n0-232f-4afc-2e9a-3832d95zc702'
);
var consent_state = new mParticle.ConsentState();
consent_state.gdpr = { document_agreement: gdpr_consent_state };
consent_state.ccpa = { data_sale_opt_out: ccpa_consent_state };
batch.consent_state = consent_state;
var commerce_event = new mParticle.CommerceEvent();
commerce_event.product_action = product_action;
commerce_event.timestamp_unixtime_ms = 1552679728376; //replace with time of transaction
commerce_event.custom_flags = {
foo: 'bar',
answer: 42,
arrays: ['foo', 'bar', 'baz']
};
batch.addEvent(commerce_event);
session_end = new mParticle.SessionEndEvent();
session_end.session_id = session_start.session_id; // it's mandatory that these match
session_end.session_duration_ms = 10000;
session_end.timestamp_unixtime_ms = 1552679728376 + 10000;
batch.addEvent(session_end);
var api = new mParticle.EventsApi(
new mParticle.Configuration('REPLACE WITH API KEY', 'REPLACE WITH API SECRET')
);
batch.user_identities = new mParticle.UserIdentities();
batch.user_identities.customerid = '123456'; // identify the user (required)
batch.user_attributes = { 'hair color': 'brown' };
batch.mpid = 600868121729048600;
batch.mp_deviceid = '59780f39-d7a0-4ebe-9950-280f937c29e2';
var data_plan = new mParticle.DataPlanContext();
data_plan.plan_id = 'docsite_v3_test';
data_plan.plan_version = 1;
var batch_context = new mParticle.BatchContext();
batch_context.data_plan = data_plan;
batch.context = batch_context;
var body = [batch]; // {[Batch]} Up to 100 Batch objects
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully.');
}
};
api.bulkUploadEvents(body, callback);
// or upload a single batch
//api.uploadEvents(body, batch)