|
832 | 832 | * - saveEvents (boolean) Whether to save events to local storage. Defaults to true. |
833 | 833 | */ |
834 | 834 | Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) { |
835 | | - this.options = options; |
836 | | - options.apiKey = apiKey; |
837 | | - if (opt_config) { |
838 | | - if (opt_config.saveEvents !== undefined) { |
839 | | - options.saveEvents = !!opt_config.saveEvents; |
840 | | - } |
841 | | - } |
842 | | - |
843 | | - // Load cookie data |
844 | | - var cookie = Cookie.get(options.cookieName); |
845 | | - var cookieData = null; |
846 | | - if (cookie) { |
847 | | - try { |
848 | | - cookieData = JSON.parse(Base64.decode(cookie)); |
849 | | - if (cookieData) { |
850 | | - if (cookieData.deviceId) { |
851 | | - options.deviceId = cookieData.deviceId; |
852 | | - } |
853 | | - if (cookieData.userId) { |
854 | | - options.userId = cookieData.userId; |
855 | | - } |
856 | | - if (cookieData.globalUserProperties) { |
857 | | - options.globalUserProperties = cookieData.globalUserProperties; |
858 | | - } |
| 835 | + try { |
| 836 | + this.options = options; |
| 837 | + options.apiKey = apiKey; |
| 838 | + if (opt_config) { |
| 839 | + if (opt_config.saveEvents !== undefined) { |
| 840 | + options.saveEvents = !!opt_config.saveEvents; |
859 | 841 | } |
860 | | - } catch (e) { |
861 | | - //log(e); |
862 | 842 | } |
863 | | - } |
864 | | - |
865 | | - options.deviceId = options.deviceId || UUID(); |
866 | | - options.userId = (opt_userId !== undefined && opt_userId !== null && opt_userId) || options.userId || null; |
867 | | - saveCookieData(); |
868 | | - |
869 | | - //log('initialized with apiKey=' + apiKey); |
870 | | - //opt_userId !== undefined && opt_userId !== null && log('initialized with userId=' + opt_userId); |
871 | | - eventId = 0; |
872 | 843 |
|
873 | | - if (options.saveEvents) { |
874 | | - var savedUnsentEventsString = localStorage.getItem(options.unsentKey); |
875 | | - if (savedUnsentEventsString) { |
| 844 | + // Load cookie data |
| 845 | + var cookie = Cookie.get(options.cookieName); |
| 846 | + var cookieData = null; |
| 847 | + if (cookie) { |
876 | 848 | try { |
877 | | - unsentEvents = JSON.parse(savedUnsentEventsString); |
| 849 | + cookieData = JSON.parse(Base64.decode(cookie)); |
| 850 | + if (cookieData) { |
| 851 | + if (cookieData.deviceId) { |
| 852 | + options.deviceId = cookieData.deviceId; |
| 853 | + } |
| 854 | + if (cookieData.userId) { |
| 855 | + options.userId = cookieData.userId; |
| 856 | + } |
| 857 | + if (cookieData.globalUserProperties) { |
| 858 | + options.globalUserProperties = cookieData.globalUserProperties; |
| 859 | + } |
| 860 | + } |
878 | 861 | } catch (e) { |
879 | 862 | //log(e); |
880 | 863 | } |
881 | 864 | } |
882 | | - } |
883 | | - if (unsentEvents.length > 0) { |
884 | | - this.sendEvents(); |
| 865 | + |
| 866 | + options.deviceId = options.deviceId || UUID(); |
| 867 | + options.userId = (opt_userId !== undefined && opt_userId !== null && opt_userId) || options.userId || null; |
| 868 | + saveCookieData(); |
| 869 | + |
| 870 | + //log('initialized with apiKey=' + apiKey); |
| 871 | + //opt_userId !== undefined && opt_userId !== null && log('initialized with userId=' + opt_userId); |
| 872 | + eventId = 0; |
| 873 | + |
| 874 | + if (options.saveEvents) { |
| 875 | + var savedUnsentEventsString = localStorage.getItem(options.unsentKey); |
| 876 | + if (savedUnsentEventsString) { |
| 877 | + try { |
| 878 | + unsentEvents = JSON.parse(savedUnsentEventsString); |
| 879 | + } catch (e) { |
| 880 | + //log(e); |
| 881 | + } |
| 882 | + } |
| 883 | + } |
| 884 | + if (unsentEvents.length > 0) { |
| 885 | + this.sendEvents(); |
| 886 | + } |
| 887 | + } catch (e) { |
| 888 | + log(e); |
885 | 889 | } |
886 | 890 | }; |
887 | 891 |
|
|
902 | 906 | }; |
903 | 907 |
|
904 | 908 | Amplitude.prototype.setUserId = function(userId) { |
905 | | - options.userId = (userId !== undefined && userId !== null && ('' + userId)) || null; |
906 | | - saveCookieData(); |
907 | | - //log('set userId=' + userId); |
| 909 | + try { |
| 910 | + options.userId = (userId !== undefined && userId !== null && ('' + userId)) || null; |
| 911 | + saveCookieData(); |
| 912 | + //log('set userId=' + userId); |
| 913 | + } catch (e) { |
| 914 | + log(e); |
| 915 | + } |
908 | 916 | }; |
909 | 917 |
|
910 | 918 | Amplitude.prototype.setGlobalUserProperties = function(globalUserProperties) { |
911 | | - options.globalUserProperties = globalUserProperties; |
912 | | - saveCookieData(); |
913 | | - //log('set globalUserProperties=' + JSON.stringify(globalUserProperties)); |
| 919 | + try { |
| 920 | + options.globalUserProperties = globalUserProperties; |
| 921 | + saveCookieData(); |
| 922 | + //log('set globalUserProperties=' + JSON.stringify(globalUserProperties)); |
| 923 | + } catch (e) { |
| 924 | + log(e); |
| 925 | + } |
914 | 926 | }; |
915 | 927 |
|
916 | 928 | Amplitude.prototype.setVersionName = function(versionName) { |
917 | | - options.versionName = versionName; |
918 | | - //log('set versionName=' + versionName); |
| 929 | + try { |
| 930 | + options.versionName = versionName; |
| 931 | + //log('set versionName=' + versionName); |
| 932 | + } catch (e) { |
| 933 | + log(e); |
| 934 | + } |
919 | 935 | }; |
920 | 936 |
|
921 | 937 | Amplitude.prototype.logEvent = function(eventType, customProperties) { |
922 | | - var eventTime = new Date().getTime(); |
923 | | - customProperties = customProperties || {}; |
924 | | - var event = { |
925 | | - device_id: options.deviceId, |
926 | | - user_id: options.userId || options.deviceId, |
927 | | - timestamp: eventTime, |
928 | | - event_id: nextEventId(), |
929 | | - session_id: -1, |
930 | | - event_type: eventType, |
931 | | - // api_properties: { |
932 | | - // location: this.getLocation() |
933 | | - // }, |
934 | | - client: BrowserDetect.browser, |
935 | | - version_code: 0, |
936 | | - version_name: options.versionName || null, |
937 | | - build_version_sdk: 0, |
938 | | - build_version_release: BrowserDetect.version, |
939 | | - // phone_manufacturer: null, |
940 | | - // phone_brand: null, |
941 | | - phone_model: BrowserDetect.OS, |
942 | | - custom_properties: customProperties, |
943 | | - global_properties: options.globalUserProperties || {} |
944 | | - // country: null, |
945 | | - // language: null, |
946 | | - // phone_carrier: null |
947 | | - }; |
948 | | - unsentEvents.push(event); |
949 | | - if (options.saveEvents) { |
950 | | - saveEvents(); |
| 938 | + try { |
| 939 | + var eventTime = new Date().getTime(); |
| 940 | + customProperties = customProperties || {}; |
| 941 | + var event = { |
| 942 | + device_id: options.deviceId, |
| 943 | + user_id: options.userId || options.deviceId, |
| 944 | + timestamp: eventTime, |
| 945 | + event_id: nextEventId(), |
| 946 | + session_id: -1, |
| 947 | + event_type: eventType, |
| 948 | + // api_properties: { |
| 949 | + // location: this.getLocation() |
| 950 | + // }, |
| 951 | + client: BrowserDetect.browser, |
| 952 | + version_code: 0, |
| 953 | + version_name: options.versionName || null, |
| 954 | + build_version_sdk: 0, |
| 955 | + build_version_release: BrowserDetect.version, |
| 956 | + // phone_manufacturer: null, |
| 957 | + // phone_brand: null, |
| 958 | + phone_model: BrowserDetect.OS, |
| 959 | + custom_properties: customProperties, |
| 960 | + global_properties: options.globalUserProperties || {} |
| 961 | + // country: null, |
| 962 | + // language: null, |
| 963 | + // phone_carrier: null |
| 964 | + }; |
| 965 | + unsentEvents.push(event); |
| 966 | + if (options.saveEvents) { |
| 967 | + saveEvents(); |
| 968 | + } |
| 969 | + //log('logged eventType=' + eventType + ', properties=' + JSON.stringify(customProperties)); |
| 970 | + this.sendEvents(); |
| 971 | + } catch (e) { |
| 972 | + log(e); |
951 | 973 | } |
952 | | - //log('logged eventType=' + eventType + ', properties=' + JSON.stringify(customProperties)); |
953 | | - this.sendEvents(); |
954 | 974 | }; |
955 | 975 |
|
956 | 976 | Amplitude.prototype.sendEvents = function() { |
|
0 commit comments