Last active
May 8, 2018 13:25
-
-
Save Nils-van-Kleef/9d65923e45f1f6d691eed791883ee668 to your computer and use it in GitHub Desktop.
[OLD] Integration of Amplitude with Optimizely. Use in Custom Analytics Integrations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"plugin_type": "analytics_integration", | |
"name": "Amplitude Analytics Integration", | |
"form_schema": [ | |
{ | |
"default_value": "[Optimizely] Experiment", | |
"field_type": "text", | |
"name": "property_prefix", | |
"label": "User Property Prefix", | |
"options": null | |
}, | |
{ | |
"default_value": "n", | |
"field_type": "dropdown", | |
"name": "sendevent", | |
"label": "Send Event", | |
"options": { | |
"choices": [ | |
{ | |
"value": "n", | |
"label": "No (not needed)" | |
}, | |
{ | |
"value": "y", | |
"label": "Yes" | |
} | |
] | |
} | |
}, | |
{ | |
"default_value": "User in Experiment", | |
"field_type": "text", | |
"name": "event_name", | |
"label": "Amplitude Event Name (optional)", | |
"options": null | |
}, | |
{ | |
"default_value": "API_KEY_DEFAULT_VALUE_HERE....", | |
"field_type": "text", | |
"name": "api_key", | |
"label": "API Key (Supply Default Value)", | |
"options": null | |
} | |
], | |
"description": "", | |
"options": { | |
"track_layer_decision": "\n//Make sure to set the API Key here and default Property Name\n\nvar AmplitudeFound=false;\n\n//Function to log an event, if the user chooses to\nfunction logEvent(){\n //console.log('Sending Amplitude Event');\n \n //Get Optimizely Campaign info and break it down into campaign name and variation name\n var campaign_info =window.optimizely.get(\"state\").getDecisionString({campaignId: campaignId});\n var campaign_name = campaign_info.substring(0,campaign_info.indexOf(\"):\")+1);\n var campaign_variation= \t\tcampaign_info.substring(campaign_info.indexOf(\"):\")+2,campaign_info.length);\n \n \n var eventProperties = {\n \t\t\t\t'[Optimizely Experiment Name]': campaign_name,\n \t\t'[Optimizely Variation Name]':campaign_variation\n\t}; \n amplitude.getInstance().logEvent(extension.event_name,eventProperties);\n \n \n}\n\n\n\n//function that runs the Identify call\nfunction IdentifyCall(){\n \n //Get Optimizely Campaign info and break it down into campaign name and variation name\n var campaign_info =window.optimizely.get(\"state\").getDecisionString({campaignId: campaignId});\n var campaign_name = campaign_info.substring(0,campaign_info.indexOf(\"):\")+1);\n var campaign_variation= \t\tcampaign_info.substring(campaign_info.indexOf(\"):\")+2,campaign_info.length);\n \n \n\n var identify = new amplitude.Identify().set(extension.property_prefix +\" \"+ campaign_name,campaign_variation);\n console.log(extension.property_prefix +\" \"+ campaign_name+\"---\"+campaign_variation);\n amplitude.getInstance().identify(identify);\n}\n\n//Function that adds Amplitude if it's not already there\nfunction AddAmplitude(){\n \n (function(e,t){var n=e.amplitude||{_q:[],_iq:{}};var r=t.createElement(\"script\");r.type=\"text/javascript\";\n r.async=true;r.src=\"https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-3.4.0-min.gz.js\";\n r.onload=function(){e.amplitude.runQueuedFunctions()};var i=t.getElementsByTagName(\"script\")[0];\n i.parentNode.insertBefore(r,i);function s(e,t){e.prototype[t]=function(){this._q.push([t].concat(Array.prototype.slice.call(arguments,0)));\n return this}}var o=function(){this._q=[];return this};var a=[\"add\",\"append\",\"clearAll\",\"prepend\",\"set\",\"setOnce\",\"unset\"];\n for(var u=0;u<a.length;u++){s(o,a[u])}n.Identify=o;var c=function(){this._q=[];return this;\n };var p=[\"setProductId\",\"setQuantity\",\"setPrice\",\"setRevenueType\",\"setEventProperties\"];\n for(var l=0;l<p.length;l++){s(c,p[l])}n.Revenue=c;var d=[\"init\",\"logEvent\",\"logRevenue\",\"setUserId\",\"setUserProperties\",\"setOptOut\",\"setVersionName\",\"setDomain\",\"setDeviceId\",\"setGlobalUserProperties\",\"identify\",\"clearUserProperties\",\"setGroup\",\"logRevenueV2\",\"regenerateDeviceId\",\"logEventWithTimestamp\",\"logEventWithGroups\"];\n function v(e){function t(t){e[t]=function(){e._q.push([t].concat(Array.prototype.slice.call(arguments,0)));\n }}for(var n=0;n<d.length;n++){t(d[n])}}v(n);n.getInstance=function(e){e=(!e||e.length===0?\"$default_instance\":e).toLowerCase();\n if(!n._iq.hasOwnProperty(e)){n._iq[e]={_q:[]};v(n._iq[e])}return n._iq[e]};e.amplitude=n;\n })(window,document);\n \n amplitude.getInstance().init(extension.api_key);\n \n}\n\n\n\n//Define function for Amplitude Tracking\nfunction fireOptlyAmplitudeTracking() {\n \n \n var seconds = 0;\n var checkExist = setInterval(function() {\n if (typeof amplitude != 'undefined') {\n //console.log(\"Amplitude Found - Setting Property\");\n IdentifyCall();\n console.log(extension.sendevent);\n if (extension.sendevent=='y') { logEvent();}\n clearInterval(checkExist);\n }\n else if (seconds==3) {\n //console.log(\"Amplitude Not Found After 3 seconds - Adding SDK\");\n AddAmplitude();\n IdentifyCall();\n if (extension.sendevent=='y') { logEvent();}\n clearInterval(checkExist);\n }\n seconds=seconds+1;\n}, 1000); // check every second\n\n\n\n}\n\n\nsetTimeout(fireOptlyAmplitudeTracking(),60000);\n\n\n" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment