(function(n){typeof define=="function"&&define.amd?define(n):n()})(function(){"use strict";function n(i,e,t,g){if(!(i instanceof Error))throw i;if(!(typeof window>"u"&&e||g))throw i.message+=" on line "+t,i;var o,r,l,d;try{g=g||require("fs").readFileSync(e,{encoding:"utf8"}),o=3,r=g.split(`
`),l=Math.max(t-o,0),d=Math.min(r.length,t+o)}catch(a){return i.message+=" - could not read from "+e+" ("+a.message+")",void n(i,null,t)}o=r.slice(l,d).map(function(a,E){var p=E+l+1;return(p==t?"  > ":"    ")+p+"| "+a}).join(`
`),i.path=e;try{i.message=(e||"Pug")+":"+t+`
`+o+`

`+i.message}catch{}throw i}function _(i){var e="",t,g;try{g=1,t="/home/circleci/project/girder/plugins/google_analytics/girder_google_analytics/web_client/templates/configView.pug",e=e+'<div class="g-config-breadcrumb-container"></div>',g=2,t="/home/circleci/project/girder/plugins/google_analytics/girder_google_analytics/web_client/templates/configView.pug",e=e+'<p class="g-google-analytics-description">',g=3,t="/home/circleci/project/girder/plugins/google_analytics/girder_google_analytics/web_client/templates/configView.pug",e=e+"To track pageviews in Girder, enter your Google Analytics tracking ID here.</p>",g=4,t="/home/circleci/project/girder/plugins/google_analytics/girder_google_analytics/web_client/templates/configView.pug",e=e+'<form id="g-google_analytics-form" role="form">',g=5,t="/home/circleci/project/girder/plugins/google_analytics/girder_google_analytics/web_client/templates/configView.pug",e=e+'<div class="form-group">',g=6,t="/home/circleci/project/girder/plugins/google_analytics/girder_google_analytics/web_client/templates/configView.pug",e=e+'<label class="control-label" for="g-google-analytics-tracking-id">',g=6,t="/home/circleci/project/girder/plugins/google_analytics/girder_google_analytics/web_client/templates/configView.pug",e=e+"Google Analytics Tracking ID</label>",g=7,t="/home/circleci/project/girder/plugins/google_analytics/girder_google_analytics/web_client/templates/configView.pug",e=e+'<input class="input-sm form-control" id="g-google-analytics-tracking-id" type="text" placeholder="Tracking ID"/></div>',g=9,t="/home/circleci/project/girder/plugins/google_analytics/girder_google_analytics/web_client/templates/configView.pug",e=e+'<p class="g-validation-failed-message" id="g-google_analytics-error-message"></p>',g=10,t="/home/circleci/project/girder/plugins/google_analytics/girder_google_analytics/web_client/templates/configView.pug",e=e+'<input class="btn btn-sm btn-primary" type="submit" value="Save"/></form>'}catch(o){n(o,t,g)}return e}const V="",f=girder.views.widgets.PluginConfigBreadcrumbWidget,m=girder.views.View,c=girder.events,{restRequest:s}=girder.rest;var y=m.extend({events:{"submit #g-google_analytics-form":function(i){i.preventDefault(),this.$("#g-google_analytics-error-message").empty(),this._saveSettings([{key:"google_analytics.tracking_id",value:this.$("#g-google-analytics-tracking-id").val().trim()}])}},initialize:function(){s({method:"GET",url:"system/setting",data:{list:JSON.stringify(["google_analytics.tracking_id"])}}).done(i=>{this.render(),this.$("#g-google-analytics-tracking-id").val(i["google_analytics.tracking_id"])})},render:function(){return this.$el.html(_()),this.breadcrumb||(this.breadcrumb=new f({pluginName:"Google Analytics",el:this.$(".g-config-breadcrumb-container"),parentView:this}).render()),this},_saveSettings:function(i){s({method:"PUT",url:"system/setting",data:{list:JSON.stringify(i)},error:null}).done(()=>{c.trigger("g:google_analytics.save"),c.trigger("g:alert",{icon:"ok",text:"Settings saved.",type:"success",timeout:4e3})}).fail(e=>{this.$("#g-google_analytics-error-message").text(e.responseJSON.message)})}});const w=girder.router,h=girder.events,{exposePluginConfig:v}=girder.utilities.PluginUtils;v("google_analytics","plugins/google_analytics/config"),w.route("plugins/google_analytics/config","googleAnalyticsConfig",function(){h.trigger("g:navigateTo",y)});const b=girder.$,u=girder.events,{restRequest:C}=girder.rest;function F(){C({method:"GET",url:"google_analytics/id"}).done(i=>{if(i.google_analytics_id){const e=i.google_analytics_id;b.getScript({url:`https://www.googletagmanager.com/gtag/js?id=${e}`,success:function(){window.dataLayer=window.dataLayer||[];function t(){dataLayer.push(arguments)}t("js",new Date),t("config",e),window.gtag=t}})}})}u.on("g:appload.after",function(){F()}),u.on("g:google_analytics.save",function(){F()}),u.on("g:hierarchy.route",function(i){let e=i.route;/^\//.test(e)||(e="/"+e),window.gtag&&window.gtag("event","page_view",{girder_route:e})}),u.on("g:navigateTo",function(i){let e=location.hash;e=e.replace(/^#/,""),window.gtag&&window.gtag("event","page_view",{girder_route:e})})});
