Skip to content

Commit b9b339d

Browse files
authored
Merge pull request #122 from Flagsmith/features/dynatrace_session_properties
3.1.1 - Dynatrace realtime user management session properties
2 parents a5f53ca + 85f5f62 commit b9b339d

File tree

21 files changed

+260
-27
lines changed

21 files changed

+260
-27
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<img width="100%" src="https://raw.githubusercontent.com/Flagsmith/flagsmith/main/static-files/hero.png"/>
2+
3+
# Flagsmith with jQuery
4+
5+
This repository contains basic integration with just jQuery.
6+
7+
## Installation
8+
9+
None! Just open index.html in your preferred browser.

examples/dynatrace-realtime-user-monitoring/index.html

Lines changed: 122 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
var environmentID = 'QjgYur4LQTwe5HpvbvhpzK'
2+
var flagsmithIdentity = "flagsmith_sample_user"
3+
4+
//Identify the user
5+
dtrum.identifyUser(flagsmithIdentity)
6+
7+
function identify() {
8+
flagsmith.identify(flagsmithIdentity)
9+
}
10+
11+
function toggleTrait () {
12+
flagsmith.setTrait('example_trait', "Some value " + Math.floor(Math.random() * 10)+"");
13+
}
14+
15+
function login () {
16+
flagsmith.identify(flagsmithIdentity);
17+
};
18+
19+
function logout () {
20+
flagsmith.logout();
21+
};
22+
function evaluateConfig () {
23+
alert(flagsmith.getValue("font_size"));
24+
};
25+
function evaluateFlag () {
26+
alert(flagsmith.hasFeature("flag")? "true":"false");
27+
};
28+
29+
$("#js-login").on("click", login);
30+
$("#js-evaluate-config").on("click", evaluateConfig);
31+
$("#js-evaluate-flag").on("click", evaluateFlag);
32+
$("#js-logout").on("click", logout);
33+
$("#js-toggle-trait").on("click", toggleTrait);
34+
35+
//Intialise Flagsmith
36+
flagsmith.init({
37+
environmentID: environmentID,
38+
cacheFlags: true,
39+
enableLogs: true,
40+
enableAnalytics:true,
41+
//specifying dtrum tells flagsmith to set session properties (see console)
42+
dtrum: typeof dtrum === 'undefined'? null: dtrum,
43+
defaultFlags: {
44+
font_size: 10
45+
},
46+
onChange: function() {
47+
$("#loaded").removeClass("hidden")
48+
$("#loading").addClass("hidden")
49+
50+
if (flagsmith.identity) {
51+
$("#logged-in").removeClass("hidden")
52+
$("#logged-out").addClass("hidden")
53+
$("#js-example-trait").text(flagsmith.getTrait("example_trait") + "");
54+
if (flagsmith.getSegments()) {
55+
$("#js-segments").text(Object.keys(flagsmith.getSegments() ).join(", "));
56+
}
57+
} else {
58+
$("#logged-out").removeClass("hidden")
59+
$("#logged-in").addClass("hidden")
60+
}
61+
$("#js-data").text(JSON.stringify(flagsmith.getAllFlags(), null, 2));
62+
}
63+
});

flagsmith-core.ts

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const Flagsmith = class {
5353
getFlags = (resolve?:(v?:any)=>any, reject?:(v?:any)=>any) => {
5454
const { onChange, onError, identity, api } = this;
5555
let resolved = false;
56-
const handleResponse = ({ flags: features, traits }, segments) => {
56+
const handleResponse = ({ flags: features, traits }) => {
5757
if (identity) {
5858
this.withTraits = false;
5959
}
@@ -75,18 +75,38 @@ const Flagsmith = class {
7575
this.oldFlags = {
7676
...this.flags
7777
};
78-
if (segments) {
79-
let userSegments = {};
80-
segments.map((s) => {
81-
userSegments[s.name] = s;
82-
});
83-
this.segments = userSegments;
84-
}
8578
const flagsEqual = deepEqual(this.flags, flags);
8679
const traitsEqual = deepEqual(this.traits, userTraits);
8780
this.flags = flags;
8881
this.traits = userTraits;
8982
this.updateStorage();
83+
if (this.dtrum) {
84+
let traits: {
85+
"javaLongOrObject": Record<string, number>,
86+
"date": Record<string, Date>,
87+
"shortString": Record<string, string>,
88+
"javaDouble": Record<string, number>,
89+
} = {
90+
javaDouble: {},
91+
date: {},
92+
shortString: {},
93+
javaLongOrObject: {},
94+
}
95+
Object.keys(this.flags).map((key)=>{
96+
setDynatraceValue(traits, "flagsmith_value_"+key, this.getValue(key) )
97+
setDynatraceValue(traits, "flagsmith_enabled_"+key, this.hasFeature(key) )
98+
})
99+
Object.keys(this.traits).map((key)=>{
100+
setDynatraceValue(traits, "flagsmith_trait_"+key, this.getTrait(key) )
101+
})
102+
this.log("Sending javaLongOrObject traits to dynatrace", traits.javaLongOrObject)
103+
this.log("Sending date traits to dynatrace", traits.date)
104+
this.log("Sending shortString traits to dynatrace", traits.shortString)
105+
this.log("Sending javaDouble to dynatrace", traits.javaDouble)
106+
this.dtrum.sendSessionProperties(
107+
traits.javaLongOrObject, traits.date, traits.shortString, traits.javaDouble
108+
)
109+
}
90110
if(this.trigger) {
91111
this.trigger()
92112
}
@@ -114,7 +134,7 @@ const Flagsmith = class {
114134
.then((res) => {
115135
// @ts-ignore
116136
this.withTraits = false
117-
handleResponse(res[0], res[1])
137+
handleResponse(res[0])
118138
if (resolve && !resolved) {
119139
resolved = true;
120140
resolve();
@@ -128,7 +148,7 @@ const Flagsmith = class {
128148
])
129149
.then((res) => {
130150
// @ts-ignore
131-
handleResponse({ flags: res[0] }, null)
151+
handleResponse({ flags: res[0] })
132152
if (resolve && !resolved) {
133153
resolved = true;
134154
resolve();
@@ -176,10 +196,10 @@ const Flagsmith = class {
176196
onError= null
177197
trigger= null
178198
identity= null
179-
segments= null
180199
ticks= null
181200
timer= null
182201
traits= null
202+
dtrum= null
183203
withTraits= null
184204

185205
init({
@@ -192,6 +212,7 @@ const Flagsmith = class {
192212
defaultFlags,
193213
preventFetch,
194214
enableLogs,
215+
dtrum,
195216
enableAnalytics,
196217
AsyncStorage: _AsyncStorage,
197218
identity,
@@ -248,6 +269,10 @@ const Flagsmith = class {
248269
throw ('Please specify a environment id');
249270
}
250271

272+
if (dtrum) {
273+
this.dtrum = dtrum;
274+
}
275+
251276
if(angularHttpClient) {
252277
_fetch = (url: string, params: { headers: Record<string, string>, method: "GET" | "POST" | "PUT", body: string }) => {
253278
const {headers, method, body} = params
@@ -424,7 +449,6 @@ const Flagsmith = class {
424449
environmentID: this.environmentID,
425450
flags: this.flags,
426451
identity: this.identity,
427-
segments: this.segments,
428452
traits: this.traits,
429453
evaluationEvent: this.evaluationEvent,
430454
}
@@ -437,7 +461,6 @@ const Flagsmith = class {
437461
this.environmentID = state.environmentID || this.environmentID;
438462
this.flags = state.flags || this.flags;
439463
this.identity = state.identity || this.identity;
440-
this.segments = state.segments || this.segments;
441464
this.traits = state.traits || this.traits;
442465
this.evaluationEvent = state.evaluationEvent || this.evaluationEvent;
443466
}
@@ -467,7 +490,6 @@ const Flagsmith = class {
467490

468491
logout() {
469492
this.identity = null;
470-
this.segments = null;
471493
this.traits = null;
472494
if (this.initialised) {
473495
return this.getFlags();
@@ -614,3 +636,16 @@ type Config= {fetch?:any, AsyncStorage?:any};
614636
export default function ({ fetch, AsyncStorage }:Config):IFlagsmith {
615637
return new Flagsmith({ fetch, AsyncStorage }) as IFlagsmith;
616638
};
639+
640+
// transforms any trait to match sendSessionProperties
641+
// https://www.dynatrace.com/support/doc/javascriptapi/interfaces/dtrum_types.DtrumApi.html#addActionProperties
642+
const setDynatraceValue = function (obj, trait, value) {
643+
let key = 'shortString'
644+
let convertToString = true
645+
if (typeof value === 'number') {
646+
key = 'javaDouble'
647+
convertToString = false
648+
}
649+
obj[key] = obj[key] || {}
650+
obj[key][trait] = convertToString ? value+"":value
651+
}

flagsmith-es/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flagsmith-es/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flagsmith-es/isomorphic.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flagsmith-es/isomorphic.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flagsmith-es/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith-es",
3-
"version": "3.1.0",
3+
"version": "3.1.1",
44
"description": "Feature flagging to support continuous development. This is an esm equivalent of the standard flagsmith npm module.",
55
"main": "./index.js",
66
"type": "module",

flagsmith-es/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export interface IInitConfig {
3232
api?: string;
3333
cacheFlags?: boolean;
3434
defaultFlags?: IFlags;
35+
dtrum?: any;
3536
enableAnalytics?: boolean;
3637
enableLogs?: boolean;
3738
angularHttpClient?: any;

0 commit comments

Comments
 (0)