|
| 1 | +<link rel="import" href="../polymer/polymer.html"> |
| 2 | +<link rel="import" href="promise-import.html"> |
| 3 | + |
| 4 | +<!-- |
| 5 | +The base element for other google analytics elements. This element is not |
| 6 | +intended to be used by itself. It is only intended to be subclassed. |
| 7 | +
|
| 8 | +@element google-analytics-base |
| 9 | +@blurb Base element for other google analytics elements |
| 10 | +@status alpha |
| 11 | +@homepage http://polymerlabs.github.io/google-analytics |
| 12 | +--> |
| 13 | + |
| 14 | +<polymer-element name="google-analytics-base"> |
| 15 | + <template> |
| 16 | + <google-api-loader |
| 17 | + name="analytics" |
| 18 | + version="v3"> |
| 19 | + </google-api-loader> |
| 20 | + </template> |
| 21 | + |
| 22 | + <script> |
| 23 | + |
| 24 | + (function() { |
| 25 | + |
| 26 | + /** |
| 27 | + * A promise that is resolved when the underlying client libraries are |
| 28 | + * loaded. It is rejected if there are any load errors. |
| 29 | + */ |
| 30 | + var loadPromise = new Promise(function(resolve, reject) { |
| 31 | + document.addEventListener('google-api-load', function fn(event) { |
| 32 | + var lib = event.detail; |
| 33 | + if (lib.name == 'analytics' && lib.version == 'v3') { |
| 34 | + resolve(event.detail); |
| 35 | + document.removeEventListener('google-api-load', fn); |
| 36 | + } |
| 37 | + }); |
| 38 | + document.addEventListener('google-api-load-error', function fn(event) { |
| 39 | + var lib = event.detail; |
| 40 | + if (lib.name == 'analytics' && lib.version == 'v3') { |
| 41 | + reject(event.detail.error); |
| 42 | + document.removeEventListener('google-api-load-error', fn); |
| 43 | + } |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + // Log any errors loading the client library. |
| 48 | + loadPromise.catch(function(err) { |
| 49 | + console.error(err.stack || err); |
| 50 | + }) |
| 51 | + |
| 52 | + |
| 53 | + Polymer('google-analytics-base', { |
| 54 | + |
| 55 | + publish: { |
| 56 | + /** |
| 57 | + * The `authorized` attribute is true when the user has signed-in and |
| 58 | + * the underlying analytics library is fully loaded. |
| 59 | + * |
| 60 | + * @attribute authorized |
| 61 | + * @type boolean |
| 62 | + */ |
| 63 | + authorized: { |
| 64 | + value: false, |
| 65 | + reflect: true |
| 66 | + } |
| 67 | + }, |
| 68 | + |
| 69 | + created: function() { |
| 70 | + document.addEventListener('google-signin-success', function(event) { |
| 71 | + loadPromise |
| 72 | + .then(this.userAuthorized.bind(this,event.detail.result)); |
| 73 | + }.bind(this)); |
| 74 | + document.addEventListener('google-signed-out', function(event) { |
| 75 | + this.userUnauthorized(); |
| 76 | + }.bind(this)); |
| 77 | + }, |
| 78 | + |
| 79 | + /** |
| 80 | + * The `userAuthorized` method will be invoked when the user has |
| 81 | + * successfully signed in and all the underlying client libraries |
| 82 | + * have been loaded. |
| 83 | + * |
| 84 | + * @method userAuthorized |
| 85 | + * @param {Object} data - The auth data information. |
| 86 | + */ |
| 87 | + userAuthorized: function(data) { |
| 88 | + this.authorized = true; |
| 89 | + }, |
| 90 | + |
| 91 | + /** |
| 92 | + * The `userUnauthorized` method will be invoked when the user has |
| 93 | + * successfully signed out. |
| 94 | + * |
| 95 | + * @method userUnauthorized |
| 96 | + */ |
| 97 | + userUnauthorized: function() { |
| 98 | + this.authorized = false; |
| 99 | + } |
| 100 | + |
| 101 | + }); |
| 102 | + |
| 103 | + }()); |
| 104 | + |
| 105 | + </script> |
| 106 | + |
| 107 | +</polymer-element> |
0 commit comments